﻿function truncateMiddle(text, length, percentOffset) {
	if (!text) {
		return '';
	}
	
	//Account for '...'
	length = Math.round(length) - 3;
	
	if (text.length > length) {
		//if no offset is provided, use half of length
		var offset = percentOffset || 50;
		firstL = Math.floor((offset / 100) * length);
		var lastL = length - firstL;
		text = text.substr(0, firstL) + '...' + text.substr(-lastL, lastL);
	}
	
	return text;
}
    
$(document).ready(function() {
		
	$("#windows_download_btn").click(function(event) {
		 var ps = document.getElementById("popup_symbian");
		 if (ps && ps.style.display == "block")
		    {
		         ps.style.display = "none";
		    }
		var pw = document.getElementById("popup_winmo");
		if (pw)
			pw.style.display = "block";
	 });
	 
	 $("#symbian_download_btn").click(function(event) {
		//  if  symbian popup exists and is open, close it
		 var pw = document.getElementById("popup_winmo");
		 if (pw && pw.style.display == "block")
		    {
		         pw.style.display = "none";
		    }
		var ps = document.getElementById("popup_symbian");
		if (ps)
			ps.style.display = "block"; 
	 });
	 
	 //    close popup for windows
	$("#popup_closebutton_windows").click(function(event) {
		var pw = document.getElementById("popup_winmo");
		 if (pw && pw.style.display == "block")
		 {
			pw.style.display = "none"; 
		 }
	 });
	 
	  //    close popup for windows
	$("#popup_closebutton_symbian").click(function(event) {
		var ps = document.getElementById("popup_symbian");
		 if (ps && ps.style.display == "block")
		 {
			ps.style.display = "none"; 
		 }
	 });	

	
	// form events
	
	var launchedCountries = new Object();
	launchedCountries.US = '1';
	launchedCountries.CA = '1';
	launchedCountries.GB = '44';
	launchedCountries.BE = '32';
	launchedCountries.AR = '54';
	launchedCountries.BR = '55';
	launchedCountries.BG = '359';
	launchedCountries.CN = '86';
	launchedCountries.CZ = '420';
	launchedCountries.EG = '20';
	launchedCountries.FR = '33';
	launchedCountries.DE = '49';
	launchedCountries.HK = '852';
	launchedCountries.IN = '91';
	launchedCountries.ID = '62';
	launchedCountries.IT = '39';
	launchedCountries.JP = '81';
	launchedCountries.KR = '82';
	launchedCountries.MY = '60';
	launchedCountries.MX = '52';
	launchedCountries.NL = '31';
	launchedCountries.PH = '63';
	launchedCountries.PL = '48';
	launchedCountries.RO = '40';
	launchedCountries.RU = '7';
	launchedCountries.SA = '966';
	launchedCountries.SG = '65';
	launchedCountries.ES = '34';
	launchedCountries.TW = '886';
	launchedCountries.TH = '66';
	launchedCountries.TR = '90';
	launchedCountries.UA = '380';
	launchedCountries.AE = '971';
	launchedCountries.VN = '84';
	
	var country = $('#country').val();
	var country_code = launchedCountries[country];
	
	$('#country_code').append($('<b>+ '+ country_code +'</b> '));
	
	$('#country').change(function() {
		country = $("#country option:selected").val();
		country_code = launchedCountries[country];
		$('#country_code').empty().append($('<b>+ '+ country_code +'</b> '));
	});
	
	
	$("#submit_img").click(function(event) {
		//alert("Country: " + $("#country").val() + " Mobile: " + $("#mobile_number").val());
		event.preventDefault();
		var mobile_number = $('#mobile_number').val(); 
		var cid = $("input[@name='cid']").val();
		var country = $('#country').val();
		
		$.ajax({
			type: "POST",
			url: "sms.php",
			data: "country_code="+country_code+"&country="+country+"&mobile_number="+mobile_number+"&cid="+cid+"",
			success: function(msg) {
				if (msg.indexOf('success') >= 0)
				{
					pageTracker._trackEvent('getskyfire', 'smssent', country_code);	
					$('#mobile_number').val('');
				}
				
				$("#block_sms").height(340);
				$("#option_content_sms").height(420);
				$("#sms_status").show();
				$("#sms_status").empty().append(msg);
			}
			
		});
	});

	
	$("#sms_form").submit(function(event) {
		event.preventDefault();

		var mobile_number = $('#mobile_number').val(); 
		var cid = $("input[@name='cid']").val();
		var country = $('#country').val();
		
		$.ajax({
			type: "POST",
			url: "sms.php",
			data: "country_code="+country_code+"&country="+country+"&mobile_number="+mobile_number+"&cid="+cid+"",
			success: function(msg) {
				if (msg.indexOf('success') >= 0)
				{
					pageTracker._trackEvent('getskyfire', 'smssent', country_code);	
					$('#mobile_number').val('');
				}
				$("#block_sms").height(375);
				$("#option_content_sms").height(425);
				$("#sms_status").show();
				$("#sms_status").empty().append(msg);
			}
			
		});
	});
	
	// handles pc download (sideload)
	$("#pc_submit_img").click(function(event) {
		event.preventDefault();
		var phone_type = $("input[@name='phone_type']:checked").val();
		var cid = $("input[@name='cid']").val();
		if(!phone_type) {
			$("#sideload_err").text('* Please make a selection first.').show();
		}
		else {
			$("#sideload_err").hide();
			pageTracker._trackEvent('getskyfire', 'downloadviapc', phone_type);	
			$.post("/download.php", {phone_type: phone_type, cid: cid}, function(data) {
					var path = data;
					var download_path = path.replace('<div class=""></div>', '');
					window.location=download_path;
				}, "text" );
			
		}
		
	});
	

    // hover triggers
    $(".section_styles").hover(
	    function () {
	    	$(this).addClass('section_styles_hover');
	    },
	    function () {
    	$(this).removeClass('section_styles_hover');
    });
    
    // email capture and email input prefill
    $('form#email_capture').submit(function(e) {
    	e.preventDefault();
    	var email = $('form#email_capture input#email').val();
    	var emailRE = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    	if ((email == 'you@email.com') || (!email.match(emailRE))) {
    		$('#email_capture-wrapper #err').show().html('*Enter a valid email.');
    	} else {
    		
    		//Hide any unsavory errors
    		$('#email_capture-wrapper #err').empty().hide();
    		
    		//Show spinny
    		$('div#email_capture-wrapper div.email_capture-mid').append('<div id="loading"></div>');
    		//Hide Submit button
    		$('div#email_capture-wrapper input#email_capture-send_btn').hide();
    		
    		function showErr(msg) {
        		$('#email_capture-wrapper #err').show().html(msg);
	    		//Remove spinny
	    		$('div#email_capture-wrapper #loading').remove();
	    		//Show Submit button
	    		$('div#email_capture-wrapper input#email_capture-send_btn').show();			
    		}
    		
    		var ajaxParams = {
				cache: false,
				type: 'GET',
				data: { email: email },
				dataType: 'text',
				timeout: 3000,
				url: 'ajax_email_submit.php',
				data: {},
				error: function(xhr, textStatus, error) {
					showErr('There was an error submitting your email.')
				},
				success: function(data) {
					if (jQuery.trim(data) == 'SUCCESS') {
						var emailStr = truncateMiddle(email, 25, 50);
						var successMsg = '<strong>SUCCESS!</strong>Your email <b>' + emailStr + '</b> was successfully submitted.';
						$('div#email_capture-wrapper div.email_capture-mid').empty().append(successMsg);
					} else {
						showErr('There was an error submitting your email.')
					}
				}
			};
    		
    		$.ajax(ajaxParams);
			
    	}
    });
    $('form#email_capture input#email').focus(function(e) {
    	//Empty input if it's focused
    	if ($('form#email_capture input#email').val() == 'you@email.com') {
    		$('form#email_capture input#email').val('');
    	}
    });
    $('form#email_capture input#email').blur(function(e) {
    	//Prefill input if it's blurred & empty
    	if ($('form#email_capture input#email').val() == '') {
    		$('form#email_capture input#email').val('you@email.com');
    	}
    });
    
});
