//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var brandmodel;
var wurflSelected;
var userAgent;
var nameDevices = new Array();
var results;
var strText;
var handsetAjaxReload;
var handsetPage;

//loading popup with jQuery magic!

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupContact").height();
	var popupWidth = jQuery("#popupContact").width();
	//centering
	jQuery("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	jQuery("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//called from the main function load popup
function loadPopup(){
	//loads popup only if it is disabled
	
	if(popupStatus==0){
		jQuery("#backgroundPopup").css({
			"opacity": "0.7"
		});
		jQuery("#backgroundPopup").fadeIn("slow");
		jQuery("#popupContact").fadeIn("slow");
		
		// used for the internalization
		strText = jQuery('#search').val();
		
		// read the recently selected devices from cookie		
		jQuery.post("/handler/HandsetSelectorHandler.php", {start: "1"}, function(data){			
				if(data.length > 0) {
					jQuery('#list').html(data);
				}
		});	
		
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#popupContact").fadeOut("slow");
		popupStatus = 0;
	
		jQuery('#search').val(strText);

		// Clean the image box		
		jQuery('#img-box').html("<p></p>");
		brandmodel = null;
		wurflSelected = null;
		userAgent = null;
	}
}

//disabling popup after a device update and reload the current page
function disablePopupAfter(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#popupContact").fadeOut("slow");
		//clean the variables
		brandmodel = null;
		wurflSelected = null;
		userAgent = null;
		
		popupStatus = 0;
	}
	
	if (handsetAjaxReload == '1') {
		jQuery.get('/view/handsetselector-container.php?page=' + handsetPage, 
			function (data) {
			 	jQuery('#boxHandSet').html(data);
			}
		);
	} else {
		window.location.reload();
	}
}

//Hide the device list
function hideScroll(){

	jQuery('#ext-gen21').hide();
}

function hideStringError(){
	jQuery('#infoText').hide();
}

//Reset the arrays
function resetArrays(){

	nameDevices = new Array();
	results = new Array();
}

//Load the Handset selector popup
function load(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
		hideScroll();
		hideStringError();	
}

//input filtering and display the device list
function lookup(search) {
		var list = '';

		search = search.toLowerCase();

		if(search.length == 0) {
			// hide the device list
			jQuery('#ext-gen21').hide();
		}else 
		if((search.length == 3) || (search == "lg")){
		
			resetArrays();
			jQuery('#ext-gen21').show();
			jQuery('#ext-gen23').html("<p><IMG SRC=\"http://c4.mobimgs.com/images2/status.gif\"></p>");	
		
			//retrieve the device list and show it
			jQuery.post("/handler/HandsetSelectorHandler.php", {queryString: ""+search+""}, function(data){	
			
				if(data.length > 0) {
					results = eval(data);
					
					//store the values into the arrays
					for(var i = 0; i < results.length; i++){
						nameDevices[i] = results[i].substring(40, results[i].indexOf("',"));
						nameDevices[i] = nameDevices[i].toLowerCase();
						
						list = list+results[i];
					}
					
					if(list != ''){
						jQuery('#ext-gen21').show();					
						jQuery('#ext-gen23').html(list);
					}else{
						hideScroll();
						brandmodel = null;
						wurflSelected = null;
						jQuery('#img-box').html("<p></p>");
					}
				}else
				{		
					hideScroll();
					brandmodel = null;
					wurflSelected = null;
					jQuery('#img-box').html("<p></p>");
				}
					
			});
		}else
		if(search.length > 3){
		
			jQuery('#ext-gen21').show();
			jQuery('#ext-gen23').html("<p><IMG SRC=\"http://c4.mobimgs.com/images2/status.gif\"></p>");
				
			if((nameDevices.length != 0) && (results.length != 0)){
				
				list = '';

				search = search.toLowerCase();
			
				//search the values from the array
				for(var j = 0; j < nameDevices.length; j++){
			
					if(nameDevices[j].search(search) != -1){
						
						list = list+results[j];
					}
				}
		
				if(list != ''){
				
					jQuery('#ext-gen21').show();
					jQuery('#ext-gen23').html(list);
				}else
				{
					hideScroll();
					brandmodel = null;
					wurflSelected = null;
					jQuery('#img-box').html("<p></p>");
				}
			}else		// case when the user click on a brand name call the php/db
			{
				list = '';
				
				jQuery('#ext-gen21').show();
				jQuery('#ext-gen23').html("<p><IMG SRC=\"http://c4.mobimgs.com/images2/status.gif\"></p>");
		
				jQuery.post("/handler/HandsetSelectorHandler.php", {queryString: ""+search+""}, function(data){			
					if(data.length > 0) {
						results = eval(data);

						//store the values into the arrays
						for(var i = 0; i < results.length; i++){
							nameDevices[i] = results[i].substring(40, results[i].indexOf("',"));
							nameDevices[i] = nameDevices[i].toLowerCase();
						
							list = list+results[i];
						}
						
						if(list != ''){
							jQuery('#ext-gen21').show();
							jQuery('#ext-gen23').html(list);
						}else
						{				
							hideScroll();
							brandmodel = null;
					    	wurflSelected = null;
						    jQuery('#img-box').html("<p></p>");
						}
					}else
					{				
						hideScroll();
						brandmodel = null;
						wurflSelected = null;
						jQuery('#img-box').html("<p></p>");
					}
				});
			}
		}
} 

// insert in the text field the selected device
function fill(thisValue,wurfl,ua) {
	    //riempi la textbox con il risultato e poi nascondi i suggerimenti
		jQuery('#search').val(thisValue);

		brandmodel = thisValue;
		wurflSelected = wurfl;
		userAgent = ua;

		displayImg(brandmodel,wurflSelected);
		
		//setTimeout("jQuery('#ext-gen21').hide();", 200);
}

//Quicklink search
function fillInText(value){
	jQuery('#search').val(value);
	resetArrays();
	lookup(value);
	hideStringError();
}

//selectRecentDevice
function chooseRecentDevice(thisValue,wurflId){
	
	jQuery.post("/handler/HandsetSelectorHandler.php", {valueMobile: ""+thisValue+"", wurflId: ""+wurflId+""}, function(data){
		if(data.length > 0) {					
			//$('#list').html(data);
			ok = data;
		}
		
		if(ok == 1){
			disablePopupAfter();
		}
	});
}

//display image when the user take the mouse over the device list
function displayImg(nameDev,wurflId){
	jQuery.post("/handler/HandsetSelectorHandler.php", {valueMobile: ""+nameDev+"", wurflId: ""+wurflId+"", displayImage: "true"}, function(data){
		if(data.length > 0) {					
			jQuery('#img-box').html(data);
		}
	});
}

function displayPrevdevice(){
	var value = jQuery('#search').val();
	
	if(value == '' || (value == strText)) {
		jQuery('#img-box').html("<p></p>");
	}else{
		if(brandmodel != '' && brandmodel){
			displayImg(brandmodel,wurflSelected);
		}else{
			jQuery('#img-box').html("<p></p>");
		}
	}
}

//Hide the text in the text area when there is a click
function hideText(){
	var value = jQuery('#search').val();
	
	if(value == strText){
		value = '';
		jQuery('#search').val(value);
	}
		
	jQuery('#infoText').hide();							
}

//Rewrite the custom text when the text box lose the focus
function defaultText(){
	var value = jQuery('#search').val();
		
	if(value == ''){
		value = strText;
		
		jQuery('#search').val(value);
		jQuery('#img-box').html("<p></p>");
		jQuery('#infoText').hide();
	}						
}

//Set the device
function setDevice(){
	var value = jQuery('#search').val();
	var ok = 0;
		
	if(value != '' && (value != strText)) {
		jQuery.post("/handler/HandsetSelectorHandler.php", {valueMobile: ""+value+"", wurflId: ""+wurflSelected+""}, function(data){
			if(data.length > 0) {					
				ok = data;///
			}
			if(ok == 1){
				disablePopupAfter();
			}else
			if(ok == 0){
				jQuery('#infoText').show();
			}
	});	 
	}else{
		jQuery('#infoText').show();
	}		
}

//send a mail
function sendMail(){
	var value = jQuery('#textmail').val();
	var seldev = jQuery('#search').val();
		
	if(value != ''){
		jQuery.post("/handler/HandsetSelectorHandler.php", {missingMobile: ""+value+"", selectphone: ""+seldev+""}, function(data){
			if(data.length > 0) {					
				
			}
		});
		disablePopup();
	}						
}

// Hide the devices list
function hideDevicesList(){	
		hideScroll();						
}

//clear the image box when the mouse is out of the device list
function clearImageBox(){
	jQuery('#img-box').html("<p></p>");
}

//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){
	
	//Click out event!
	jQuery('#backgroundPopup').click(function(){
		disablePopup();
	});
	
	//Press Escape event!
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
});