var globalObj = {}; //global object
$(function() {
	Z.Global = {
		winpop : new Array(),
		supplierClick: new Array(),
		getEmailCallback: null,
		checkPopupBlocker: false,
		prepop: new Array(),
		ie : 'Microsoft Internet Explorer',
		//ie : 'Netscape',
		prepopIndex: 0,
		createPrePopWinLimit: 1,
		item_selected:0,
		dt_advertiser_url: window.location.protocol+'//'+document.domain+'/post/get-dt-advertiser.php',
		dt_Ads: null,
		init:function(){
			var El = this;
			$.ajax({
				url: El.dt_advertiser_url,
				//dataType: 'json',
				success: function(res){
					El.dt_Ads = jQuery.parseJSON(res);
					auto();
				}
			});
			$("#various1").fancybox({
				'type':'inline',
				'showCloseButton':false,
				'autoDimensions': true, 
				'autoScale': true,
				//'width' : screen.availWidth-42,
				'height' : screen.availHeight-170,
				onClosed : function(){
					//El.winpop = new Array();
					$('#sticky-ads').show();
				},
				'onComplete': function() {
					//$("#fancybox-wrap").css({'top':'20px', 'bottom':'auto'});
					$("#fancybox-wrap").css({'left':'0px','top':'0px', 'bottom':'auto'});
				},
				onStart: function(){
					$("#fancybox-wrap").css({'left':'0px !important','top':'0px !important', 'bottom':'auto'});
				}
			});
			
			$('#popup-close').click(function(){
				$.fancybox.close();
				$('#sticky-ads').slideDown();
			});
			
			$('.close1').click(function() {
				$.unblockUI();				
				setTimeout(function(){
					El.getEmailCallback.compare(El.getEmailCallback);
				},500);
				return false; 
			}); 
			
			$('.close12').click(function() {
				$.unblockUI();				
				return false; 
			}); 
			
			$('.subs-in-news-btn').click(function(){
				El.getEmailCallback.subscribe(El.getEmailCallback);
				$('.close1').trigger('click');
				return false;
			});
			
			$('#searc-more').click(function(){
				$('#sticky-ads').hide();
				var city1 = $('div#tab-1 #city1').val();
				var city2 = $('div#tab-1 #city2').val();
				var date1 = $('div#tab-1 #date1').val();
				var date2 = $('div#tab-1 #date2').val();
				var adults = $('div#tab-1 #adults').val();
				var flighttype = ($('div#tab-1 #one_way').attr('checked')?1:2);
				
				/*pattern = new RegExp(/\(([^}]+)\)/);
				var newString1 = pattern.exec(city1);
				var newString2 = pattern.exec(city2);
				var city1 = (newString1!=undefined)?newString1[1]:city1
				var city2 = (newString2!=undefined)?newString2[1]:city2*/
				//if(validateData()){
					window.location.replace('http://hdn.com/?'+('auto=1&city1='+city1+
						'&city2='+city2+'&date1='+date1+'&date2='+date2+'&flighttype='+flighttype+'&adult='+adults));
				//}
				return false;
			});
		},
		isDate: function (dateStr) {
			var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
			var matchArray = dateStr.match(datePat); // is the format ok?
			if (matchArray == null) {
				return false;
			}
			var month = matchArray[1]; // p@rse date into variables
			var day = matchArray[3];
			var year = matchArray[5];
			if (month < 1 || month > 12) { // check month range
				alert("Month must be between 1 and 12.");
				return false;
			}
			if (day < 1 || day > 31) {
				alert("Day must be between 1 and 31.");
				return false;
			}
			if ((month==4 || month==6 || month==9 || month==11) && day==31) {
				alert("Month "+month+" doesn`t have 31 days!")
				return false;
			}
			if (month == 2) { // check for february 29th
				var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
				if (day > 29 || (day==29 && !isleap)) {
					alert("February " + year + " doesn`t have " + day + " days!");
					return false;
				}
			}
			return true; // date is valid
		},
		popSBS: function (rec,url,_id,_sArray) {
			var self = this;
			var topX=_sArray[rec].left;
			var topY=_sArray[rec].top;
			var sWidth = _sArray[rec].width;
			var sHeight = _sArray[rec].height;
			var strOptions = 'toolbar,menubar,scrollbars,resizable,location,height=' + sHeight + ',width=' +
				sWidth + ',left=' + topX + ',top=' + topY + ',screenX=' + topX + ',screenY=' + topY;
				
			//window.blur();
			if(self.winpop[_id]){
				try {
					self.winpop[_id].moveTo(topX, topY);
					self.winpop[_id].resizeTo(sWidth,sHeight);
					self.winpop[_id].focus();
					
					self.winpop[_id].location.href = url;
					self.winpop[_id].focus();
				}catch(e){}
			}else{
				self.winpop[_id] = window.open(url, 'win_'+_id, strOptions);
				/*if(self.winpop[_id] == null || typeof(self.winpop[_id])=='undefined'){
					alert('Warning! Your Pop-Up Blocker is On.');
				}*/
				//self.winpop[_id].onclose = function (){alert('sss');}
			}
		},
		
		popStacked: function  (rec,url,_id) {
			var self = this;
			var topX=200*rec;
			var topY=100*rec;
			var sWidth = 400;
			var sHeight = 400;
			var to = 100;
			if (navigator.appName == 'Microsoft Internet Explorer'){
				to = window.screenTop;
			}else if(navigator.appName == 'Netscape'){
				to = window.mozInnerScreenY;
			}else{
				to = window.outerHeight - window.innerHeight;
			}
			var myWidth = (screen.availWidth > 800) ? (screen.availWidth - 100) : screen.availWidth;		// if 800x600 or less, take up
			var myHeight = (screen.availHeight > 600) ? (screen.availHeight - 100) : (screen.availHeight - 70);	// the whole screen.
			var myLeft = Math.round((screen.availWidth - myWidth) / 2);
			var myTop = Math.round((screen.availHeight - myHeight) / 2)-25;
			to = myTop;
			//if (navigator.appName == ie){
				//var s =60+parseInt(window.screen.availWidth / 4);
				var s =10+parseInt($('#fancybox-wrap').css('width').replace('px',''));
				
				//topX=s+(40*rec);
				topX=s+(100*rec);
				topY=to+(20*rec);
			//}
			
			sWidth = window.screen.availWidth - topX;
			sHeight = window.screen.availHeight -  topY;
			
			sWidth = ( screen.width * .5 );
			sHeight = ( screen.height * .5 );
						
			var strOptions = 'toolbar,menubar,scrollbars,resizable,location,height=' + sHeight + ',width=' +
				sWidth + ',left=' + topX + ',top=' + topY + ',screenX=' + topX + ',screenY=' + topY;
			
			//window.blur();
			/*if(self.winpop[_id]){
				try{
					self.winpop[_id].moveTo(topX, topY);
					self.winpop[_id].resizeTo(sWidth,sHeight);
					self.winpop[_id].focus();
					
					self.winpop[_id].location.href = url;
					self.winpop[_id].focus();
				}catch(e){}
			}else{*/
			
				if(self.winpop[_id]){
					self.winpop[_id].close();
					self.winpop[_id] = undefined;
				}
		
				self.winpop[_id]= window.open(url, 'win_'+_id, strOptions);
				
				if(self.checkPopupBlocker==false){
					self.checkPopupBlocker = true;
					var popup = window.open('','_blank','width=10, height=10, left='+screen.width+', top='+screen.height+',toolbar=no,scrollbars=no,resizable,location');
					if(popup == null || typeof(popup)=='undefined'){
						$.blockUI({ message: $('#popup-blocker-msg'),
							baseZ:2000,
							css: {
								top: '41px',
								margin: '0 0 0 250px',
								border: 'none', 
								padding: '0px', 
								cursor: 'default'
							}
						});
					}else{
						popup.close();
					}
				}
			//}
		},
		popStackedIE:function (rec,url,_id,El) {
			var self = this;
			var topX=200*rec;
			var topY=100*rec;
			var sWidth = 400;
			var sHeight = 400;
			var to = 100;
			if (navigator.appName == 'Microsoft Internet Explorer'){
				to = window.screenTop;
			}else if(navigator.appName == 'Netscape'){
				to = window.mozInnerScreenY;
			}else{
				to = window.outerHeight - window.innerHeight;
			}
			var myWidth = (screen.availWidth > 800) ? (screen.availWidth - 100) : screen.availWidth;		// if 800x600 or less, take up
			var myHeight = (screen.availHeight > 600) ? (screen.availHeight - 100) : (screen.availHeight - 70);	// the whole screen.
			var myLeft = Math.round((screen.availWidth - myWidth) / 2);
			var myTop = Math.round((screen.availHeight - myHeight) / 2)-25;
			to = myTop;
			
			var w1 = $('#fancybox-wrap').css('width');
			var w2 = (w1 * window.screen.availWidth / 100);

				var s = 10+parseInt(w1);
				//topX=s+(40*rec);
				topX=s+(100*rec);
				topY=to+(20*rec);
			
			sWidth = window.screen.availWidth - topX;
			sHeight = window.screen.availHeight -  topY;
			
			sWidth = ( screen.width * .5 );
			sHeight = ( screen.height * .5 );
				
			var strOptions = 'toolbar=no,scrollbars=no,resizable,location,height=' + sHeight + ',width=' +
				sWidth + ',left=' + topX + ',top=' + topY + ',screenX=' + topX + ',screenY=' + topY;
			
			//window.blur();
			if(self.prepop[_id]){
				try{
					self.prepop[_id].moveTo(topX, topY);
					self.prepop[_id].resizeTo(sWidth,sHeight);
					self.prepop[_id].focus();
					
					self.prepop[_id].location.href = url;
					self.prepop[_id].focus();
					return true;
				}catch(e){}
			}else{
				return false;
			}
		},
		updateMsgCompare: function (el){
			var El = el;
			var title = new Array();
			
			$("#"+El.contentId+' #selected .result-box').each(function(i,v){			
				title.push($(this).find('h3.back-title').html()+' '+$(this).find('input.group').val());			
			});
			
			var tmpstr = '';
			if(selectedLink==1){
				tmpstr = title[0];
			}else if(selectedLink==2){
				tmpstr = title[0]+ ' and ' +title[1];
			}else if(selectedLink==3){
				tmpstr = title[0]+ ', '+title[1]+' and ' +title[2];
			}else if(selectedLink==4){
				tmpstr = title[0]+ ', '+title[1]+', '+title[2]+' and ' +title[3];
			}else{
				$("#"+El.contentId+' #msg-compare1').html('<h1>Select 3 or more site to compare</h1>');
				return;
			}
			$("#"+El.contentId+' #msg-compare1').html('<span style="">Comparing&nbsp;</span><b >'+tmpstr+'</b>&nbsp;&nbsp;&nbsp;');
		},
		setResultBox: function(el){
			var El = el;
			var self = this;
			$("#"+El.contentId+' .result-box').click(function(){
				var selectLimit = 4;
				if($(this).find('.result-box-icon').is(":visible")){
					if(selectedLink==4) return;
					selectedLink++;
					var clone = $(this).clone();
					var obj = $(this).find('img.check')[0];
		
					if(obj!=undefined){
						
						var src = $(clone).find('.result-box-btn img').attr('src').replace('btn-plus.png','btn-x.png');
						$(clone).find('.result-box-btn img').attr('src',src);
					}
					$(clone).find('.advertiser').show();
					$(clone).find('a').hide();
					$(clone).find('.check2').show();
					$(clone).find('.title2').show();
					var id = this.id;
					$("#"+El.contentId+' #selected').prepend(clone);
					
					var selectRemaining = selectLimit - selectedLink;
					if(selectRemaining==0)
						$("#"+El.contentId+' #selectNotification').html('<b>You are now ready to compare.</b>');
					else if(selectRemaining==1)
						$("#"+El.contentId+' #selectNotification').html('<b>Add '+selectRemaining+' more site or continue to compare.</b>');
					else
						$("#"+El.contentId+' #selectNotification').html('<b>Add '+selectRemaining+' more sites to compare.</b>');
					
					self.updateMsgCompare(El);
					
					$("#"+El.contentId+' #selected').find('#'+id).click(function(){
						var objleft = $('.box-result-top').find('div#'+this.id.replace('-selected',''));
						objleft.children().show();
						
						objleft.find('.back-title').hide();
						objleft.css({'background-color':'white'});
											
						selectedLink--;
						if(selectedLink<1){
							selectedLink=0;										
						}
						
						var selectRemaining = selectLimit - selectedLink;
						if(selectRemaining==0)
							$("#"+El.contentId+' #selectNotification').html('<b>You are now ready to compare.</b>');
						else if(selectRemaining==1)
							$("#"+El.contentId+' #selectNotification').html('<b>Add '+selectRemaining+' more site or continue to compare.</b>');
						else if(selectRemaining==4)
							$("#"+El.contentId+' #selectNotification').html('');
						else
							$("#"+El.contentId+' #selectNotification').html('<b>Add '+selectRemaining+' more sites to compare.</b>');
						
						self.updateMsgCompare(El);
						
						$(this).remove();
						$("#"+El.contentId+' #selected').append('<div id="" style="height: 60px;background:url(http://fliasia.s3.amazonaws.com/fliasia-2/images/selectbutton-small.png) #EFEFEF no-repeat center center !important;" class="result-box box"></div>');
						
						//for select site box
						var index = $("#"+El.contentId+' #selected').children().length-1;
						$($("#"+El.contentId+' #selected').children()[index]).click(function(){
							$($("#"+El.contentId+' #search-result .result-box .result-box-icon:visible')[0]).trigger('click');
						});
						//--					
					});
					
					$($("#"+El.contentId+' .box')[0]).remove();
					
					$(this).css({'background-color':'#EFEFEF','border-color':'#E3CEA0'});
					$(this).children().hide();
					$(this).find('.back-title').show();
					
					var strOptions = 'toolbar,menubar,scrollbars,resizable,location,height=' + 100 + ',width=' + 100 + ',left=' + 10 + ',top=' + 10 + ',screenX=' + 10 + ',screenY=' + 10;
					var id_ = id.replace('select','');
					id_ = id_.replace('-selected','');				
					
					/*switch($(this).find('.back-title')[0].id){
						case 'Orbitz':
						case 'CheapTickets':
						case 'BookingBuddy':
						case 'TripAdvisor-ca':
						case 'Shermans Travel':
						case 'Travelation-Com':
						case 'Trip-com':
							if(self.winpop[id_]){
								self.winpop[id_].close();
							}
							self.winpop[id_] = window.open('', 'win_' + id_, strOptions);
							self.winpop[id_].blur();
							window.focus();
						break;
					}*/
					if(navigator.appName == El.ie){
						self.createPrePopWin();
						/*if(self.winpop[id_]){
							self.winpop[id_].close();
						}
						var ww = window.open('', 'win_' + id_, strOptions);
						if(self.winpop[id_] != null){
							self.winpop[id_] = ww;
						}
						
						if(ww == null || typeof(ww)=='undefined'){
							$.blockUI({ message: $('#popup-blocker-msg'),
								baseZ:2000,
								css: {
									top: '41px',
									margin: '0 0 0 250px',
									border: 'none', 
									padding: '0px', 
									cursor: 'default'
								}
							});
							
							$("#"+El.contentId+' #selected .result-box').each(function(i,v){
								var obj = $(v).find('#url');
								if(obj[0] != undefined){ //trigger click on not empty box
									$(this).trigger('click');
								}
							});	
							return false;
						}							
						self.winpop[id_].blur();*/
						window.focus();
					}
				}else{
					return;
				}
			});
		
			$("#"+El.contentId+' .result-box').hover(
				function(){					
					$(this).css({'border-color':'green','border-width':'2px','cursor': 'pointer'});
				},
				function(){
					$(this).css({'border-color':'#E3CEA0','border-width':'2px','cursor': 'default'});
				}
			);
		},
		clickSelected: function(_el){
			var self = this;
			
			if($(_el).data('click') == undefined){
				self.item_selected--;
				if(self.item_selected<=0)
					$('#item-checked').hide();
				else
					$('#item-checked').html(self.item_selected+' more sites to check');
				
				$(_el).data('click',true);
			}
							
							
			$('.mb-check-icon').removeClass('active-tab');
			$('.mb-box-icon').removeClass('active-tab');
			$(_el).parent().removeClass('def-tab');
			$(_el).parent().addClass('active-tab');
			
			/*$('.mb-check-icon').removeClass('active-tab');
			$('.mb-box-icon').removeClass('active-tab');
			$(_el).parent().removeClass('def-tab2');
			$(_el).parent().removeClass('def-tab');
			$(_el).parent().addClass('active-tab');
			
			$(_el).parent().find('.def-tab2-img').hide();*/
					
			var id = $(_el)[0].id.replace('sup-win','');
			self.supplierClick[id] = true;
			
			if(navigator.appName == self.ie){
				var prePopId = $(_el).data('prepop');
				
				if(globalObj.prepop[prePopId]!=undefined){
					globalObj.prepop[prePopId].focus();
				}else{
					var url = $(_el).find('._url').val();
					
					var myWidth = (screen.availWidth > 800) ? (screen.availWidth - 100) : screen.availWidth;		// if 800x600 or less, take up
					var myHeight = (screen.availHeight > 600) ? (screen.availHeight - 100) : (screen.availHeight - 70);	// the whole screen.
					
					var myTop = Math.round((screen.availHeight - myHeight) / 2)-25;
					var winLeft =10+parseInt($('#fancybox-wrap').css('width').replace('px',''));
					var windowWidth = ( screen.width * .5 );
					var windowHeight = ( screen.height * .5 );
					var strOptions = 'toolbar=no,scrollbars=no,resizable,location,height=' + windowHeight + ',width=' +
						windowWidth + ',left=' + winLeft + ',top=' + myTop + ',screenX=' + winLeft + ',screenY=' + myTop;
					
					globalObj.prepop[id] = window.open(url, 'win_'+id, strOptions);									
				}
			}else{
				if(self.winpop[id]!=undefined){
					self.winpop[id].focus();
				}
			}
			
			var allClicked = true;
			for(var o =0;o<self.supplierClick.length;o++){
				if(self.supplierClick[o] == false){
					allClicked = false;
					break;
				}
			}
			if(allClicked){
				if($('#mb-top-check-right-unselected').children().length>0)
					$('#more-supplier').slideDown();
			}
		},
		clickUnselected: function(_el){
			var self = this;
			$('.mb-check-icon').removeClass('active-tab');
			$('.mb-box-icon').removeClass('active-tab');
			$(_el).parent().removeClass('def-tab');
			$(_el).parent().addClass('active-tab');
			
			var id = $(_el)[0].id.replace('sup-win','');
			var obj = $(_el).find('._url');
			var url = obj.val();
			
			if($(_el).data('click') == undefined){
				$(_el).data('click',true);
				var newId = new Date();
				$(_el).data('windId',newId.getTime());
			}
			
			id = $(_el).data('windId');
			
			if(self.winpop[id]!=undefined){
				self.winpop[id].focus();
			}else{
				var myWidth = (screen.availWidth > 800) ? (screen.availWidth - 100) : screen.availWidth;		// if 800x600 or less, take up
				var myHeight = (screen.availHeight > 600) ? (screen.availHeight - 100) : (screen.availHeight - 70);	// the whole screen.
				
				var myTop = Math.round((screen.availHeight - myHeight) / 2)-25;
				//var winLeft = (10+parseInt(window.screen.availWidth / 4));
				//var windowWidth = window.screen.availWidth - (60+parseInt(window.screen.availWidth / 4))+50;
				var winLeft =10+parseInt($('#fancybox-wrap').css('width').replace('px',''));
				//var windowWidth = window.screen.availWidth - winLeft;
				//var windowHeight = screen.availHeight;
				
				var windowWidth = ( screen.width * .5 );
				var windowHeight = ( screen.height * .5 );
						
				var strOptions = 'toolbar,menubar,scrollbars,resizable,location,height=' + windowHeight + ',width=' +
					windowWidth + ',left=' + winLeft + ',top=' + myTop + ',screenX=' + winLeft + ',screenY=' + myTop;
				try {
					if(self.winpop[id]){
							self.winpop[id].focus();				
					}else{
						self.winpop[id] = window.open(url, 'win_'+id, strOptions);
					}
				}catch(e){return false;}
			}
		},
		compare:function(el){
			var self = this;
			var El = el;
			var n = 0;
			self.item_selected = 0;
			sArray = new Array();
			self.checkPopupBlocker = false;
			n = $("#"+El.contentId+' #selected .result-box').length - $("#"+El.contentId+' #selected .box').length;//minus empty box
							
			if($("#"+El.contentId+' .trip_inside3 .result-box').length==1){				
				if(n<1){
					$("#"+El.contentId+' .error-notification').show();
					setTimeout(function(){
						$("#"+El.contentId+' .error-notification').fadeOut('slow');
					},3500);
					return;
				}
			}				
			
			if($("#"+El.contentId+' .trip_inside3 .result-box').length==2){
				if(n!=2){
					$("#"+El.contentId+' .error-notification').show();
					setTimeout(function(){
						$("#"+El.contentId+' .error-notification').fadeOut('slow');
					},3500);
					return;
				}
			}				
			
			if($("#"+El.contentId+' .trip_inside3 .result-box').length>2){
				 if($("#"+El.contentId+' #search-result .result-box').length>2&&n<=2){
					$("#"+El.contentId+' .error-notification').show();
					setTimeout(function(){
						$("#"+El.contentId+' .error-notification').fadeOut('slow');
					},3500);
					return;
				}else if($("#"+El.contentId+' #search-result .result-box').length<3&&n<=1){
					$("#"+El.contentId+' .error-notification').show();
					setTimeout(function(){
						$("#"+El.contentId+' .error-notification').fadeOut('slow');
					},3500);
					return;
				}
			}
			
			// double row window
			//var nWindowsHor = n == 4 ? 2 : n;
			//var nWindowsVer = n == 4 ? 2 : 1;
			
			// single row window
			var nWindowsHor = n;
			var nWindowsVer = 1;
			
			//var windowWidth = screen.availWidth / nWindowsHor;
			var winLeft = (10+parseInt(window.screen.availWidth / 4));
			var windowWidth =50+ (window.screen.availWidth - (60+parseInt(window.screen.availWidth / 4)))/nWindowsHor;
			var windowHeight = screen.availHeight / nWindowsVer;
			for (i = 0; i <n; i++) {
				var c = new Object;
				
				c.left = Math.floor(winLeft+((i % nWindowsHor) * windowWidth + 0.5));
					
				c.top = Math.floor(Math.floor(i / nWindowsHor) * windowHeight + 0.5);
				//c.width = Math.floor(windowWidth + 0.5);
				c.width = Math.floor(windowWidth);
				c.height = Math.floor(windowHeight + 0.5);
				sArray[i]=c;
			}
			var ii = 0;
			$("#"+El.contentId+' .tabs').html('');
			$("#"+El.contentId+' .tab_container').html('');
			
			popw = new Array();
			popX=1;
			popY=1;
			
			$('#various1').trigger('click');
			//$('#getEmail').trigger('click');
			
			$('#item-checked').html('').show();
			$('#mb-top-check-right-selected').html('');
			$('#mb-top-check-right-unselected').html('');
			
			var count_unselected = 0;
			$("#"+El.contentId+' #search-result .result-box').each(function(i,v){ //append unselected ads to modal popup
				if($(this).find('.result-box-icon').is(':visible')){
					var type__ = $(v).find('.title2').html();
					var obj = $(v).find('#url');
					var url ='';
						
					if(El.staticURL =='')
						url = obj.val();
					else
						url = El.staticURL;
						
					var img = $(v).find('.advertiser')[0].src;
					var sup = '<div class="mb-box-icon"><a id="sup-win'+
						this.id.replace('select','')+'" href="javascript:;" class="sup-unselected"><img width="80" height="34" alt=" " src="'+img+'">'+
						'<input type="hidden" class="_url" value="'+url+'" />'+'</a>'+
						'<span class="search-type" >'+type__+'</span>'+
						'</div>';
					$('#mb-top-check-right-unselected').append(sup);
					count_unselected++;
				}
			});
			$("#"+El.contentId+' #search-result-flight .result-box').each(function(i,v){ //append unselected ads to modal popup
				if($(this).find('.result-box-icon').is(':visible')){
					var type__ = $(v).find('.title2').html();
					var obj = $(v).find('#url');
					var url ='';
						
					if(El.staticURL =='')
						url = obj.val();
					else
						url = El.staticURL;
						
					var img = $(v).find('.advertiser')[0].src;
					var sup = '<div class="mb-box-icon"><a id="sup-win'+
						this.id.replace('select','')+'" href="javascript:;" class="sup-unselected"><img width="80" height="34" alt=" " src="'+img+'">'+
						'<input type="hidden" class="_url" value="'+url+'" />'+'</a>'+
						'<span class="search-type" >'+type__+'</span>'+
						'</div>';
					$('#mb-top-check-right-unselected').append(sup);
					count_unselected++;
				}
			});

			$('#sites-to-check').html(count_unselected+' more sites to check');
			$('.sup-unselected').click(function(i,v){
				
				self.clickUnselected(this);				
				
				return false;
			});
			
			self.supplierClick = new Array();
			var count_selected = 0;
			var lastItemID = '';
			
			var arr = self.getAvailablePopWin();
			
			var arrOTA = new Array();
			var META = new Array();
			
			$("#"+El.contentId+' #selected .result-box').each(function(i,v){
				var tmp = $(v).find('#classification').val();
				
				if(tmp == 'OTA'){
					arrOTA.push(v);
				}else{
					META.push(v);
				}
			});
			
			for(var x=0;x<META.length;x++){
				arrOTA.push(META[x]);
			}
			
			//$("#"+El.contentId+' #selected .result-box').each(function(i,v){ //append selected ads
			$(arrOTA).each(function(i,v){ //append selected ads
				var obj = $(v).find('#url');
				if(obj[0] != undefined){ 
					var title__ = $(v).find('h3')[0].id;
					var img = $(v).find('.advertiser')[0].src;
					var type__ = $(v).find('.title2').html();
					var _id_ = v.id.replace('select','');
					var url ='';
					
					if(El.staticURL =='')
						url = obj.val();
					else
						url = El.staticURL;
						
					var divClass = 'def-tab';
					var winpopId_ = '';
					if(navigator.appName == El.ie){
						winpopId_ = arr.pop();
						if(self.prepop[winpopId_]){
							//
						}else{
							divClass = 'def-unpopped';
						}
					}
					
					//var sup = '<div class="mb-check-icon def-tab"><a id="sup-win'+
					var sup = '<div class="mb-check-icon '+divClass+'"><a id="sup-win'+
					this.id.replace('select','')+'" href="javascript:;" class="sup"><img width="80" height="34" alt=" " src="'+img+'">'+
						'<input type="hidden" class="_url" value="'+url+'" />'+'</a>'+
						'<span class="search-type" >'+type__+'</span>'+'</div>';
					
					/*var sup = '<div class="mb-check-icon def-tab2"><a id="sup-win'+
					this.id.replace('select','')+'" href="javascript:;" class="sup"><img width="80" height="34" alt=" " src="'+img+'">'+
						'<input type="hidden" class="_url" value="'+url+'" />'+'</a>'+
						'<span class="search-type" >'+type__+'</span>'+
						'<img class="def-tab2-img" src="http://hotels.asia.com/images/ajax-loader4.gif" />'+
						'</div>';*/
						
						$('#mb-top-check-right-selected').append(sup);
					
						if($('#dispCompare_sbs').is(':checked')===true) { 					
							self.popSBS(ii ,url, this.id.replace('select',''),sArray);				
						}else{
							//for ie
							if(navigator.appName == El.ie){
								$('#mb-top-check-right-selected').children(':last').find('.sup').data('prepop',winpopId_);
								self.popStackedIE(ii,url, winpopId_,El);
								
							}else{
								self.popStacked(ii,url, this.id.replace('select',''));
							}
						}
					self.supplierClick[this.id.replace('select','')] = false;
					ii++;
					count_selected++;
					lastItemID = this.id.replace('select','');
				}
				
			});
			
			//$('.mb-check-icon:last').removeClass('def-tab').addClass('active-tab');
			//supplierClick[lastItemID] = true;
			
			$('.mb-check-icon:first').removeClass('def-tab').addClass('active-tab');
			
			$('.mb-check-icon:first').find('.sup').data('click',true);
			
			//$('.mb-check-icon:first').removeClass('def-tab2').addClass('active-tab');
			//$('.mb-check-icon:first').find('.def-tab2-img').hide();
			
			var lid = $('.mb-check-icon:first').children()[0].id.replace('sup-win','');
			$('#iframe-'+lid).show();
			self.supplierClick[lid] = true;

			$("#"+El.contentId+' #checking-sites').html('Checking '+count_selected+' sites');
			
			self.item_selected = count_selected - 1;
			
			if(self.item_selected>0)
				$('#item-checked').html(self.item_selected+' more sites to check');
						
			$('.mb-check-icon').click(function(){
				$(this).find('.sup').trigger('click');
			});
			
			$('.sup').click(function(){
				self.clickSelected(this);
				
				return false;
			});
			
			$('.mb-check-icon').click(function(){
				$(this).find('a').trigger('click');
				return false;
			});
			
			$('.mb-box-icon').click(function(){
				$(this).find('a').trigger('click');
				return false;
			});
			
			$("#"+El.contentId+' #selected .result-box').each(function(i,v){
				var obj = $(v).find('#url');
				if(obj[0] != undefined){ //trigger click on not empty box
					$(this).trigger('click');
				}
			});	
			
			selectedLink = 0;
			$("#"+El.contentId+' #msg-compare1').html('<h1>Pick 3 or more favorite sites</h1>'+'<h1 class="msg2">You can now compare again or Edit your trip.</h1>');
			
		},
		displayResult:function(El,child,caption,xId,section,pop_url){
			//var xId = 0;
			var arrOTA = new Array();
			var META = new Array();
			
			for(var x=0;x<$(child).length;x++){
				var name = $(child)[x]['supplier-name'].toLowerCase();
				var tmp = globalObj.getAdsClassification(name);
				
				if(tmp == 'OTA'){
					arrOTA.push($(child)[x]);
				}else{
					META.push($(child)[x]);
				}
			}
			
			for(var x=0;x<META.length;x++){
				arrOTA.push(META[x]);
			}
			
			//for(var x=0;x<$(child).length;x++){
			for(var x=0;x<arrOTA.length;x++){
				var item=$('<div class="result-box-icon"></div>');
				var filename = arrOTA[x]['supplier-name'];
				if(filename!=undefined){
					
					var filename_ = filename.replace('.','-');
					var title = $('<h3 id="'+filename_+'" style="display:none;" class="back-title">'+filename+'</h3>');
					var group = $('<input class="group" type="hidden" style="display:none;" value="'+caption+'"></h3>');
					var url_val = pop_url + "&company=bw&advertiser="+encodeURIComponent(filename)+"&url="+encodeURIComponent(arrOTA[x]['url']);
					
					//var url = $("<input id='url' type='hidden' value='"+arrOTA[x]['url']+"' />");
					var url = $("<input id='url' type='hidden' value='"+url_val+"' />");
					item.append(url);
					
					var classification = $("<input id='classification' type='hidden' value='"+
						globalObj.getAdsClassification(arrOTA[x]['supplier-name'].toLowerCase())+"' />");
					
					var imgUrl = arrOTA[x]['image-link'];
					var arr = imgUrl.split('/');
					var ti = arr[arr.length-1].split('-')[0];
					ti = (ti!=undefined)?ti:'';
					ti = ti.replace('_',' ');
					ti = ti.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
					ti = ti.toLowerCase();
					ti = ti.split(' ')[0];
					ti = ti.replace(/^\s+|\s+$/g,"");
					imgUrl = "http://fliasia.s3.amazonaws.com/widget-wide/images/advertisers/logo-"+ti+".png";
					var chk = '<input type="checkbox" style="float: left; margin: 16px 0px 10px 0pt;" id="checkbox-0" class="checkboxes">';
					item.append($("<img class='advertiser' src ='"+imgUrl+"' alt='Search "+filename+" "+caption+"' title='Search "+filename+" "+caption+"'/>"));
					item.append($("<h1 class='title2' style='display:none;' >"+" "+caption+"</h1>"));
					
					var item2 = $("<div class='result-box-btn'></div>");
					
					var img = '<img style="cursor:pointer" class="check" src="http://assets.zipsite.net.s3.amazonaws.com/images/marvinpogi/btn-plus.png"/>';
					item2.append($(img));
					var result_box = $("<div id=select"+xId+" class='result-box'></div>");
					result_box.append(item);
					result_box.append(item2);
					result_box.append(title);
					result_box.append(group);
					result_box.append(classification);
					$("#"+El.contentId+' #'+section).append(result_box);								
				}
				xId++;
			}
			return xId;
		},
		createPrePopWin:function(check){
			var El = this;
			//for ie---
			if(navigator.appName == El.ie && globalObj.createPrePopWinLimit<5){
				globalObj.prepopIndex++;
				if(check==undefined || check ==false){
					globalObj.createPrePopWinLimit++;
				}
				globalObj.prepop[globalObj.prepopIndex] = window.open('','_blank','width=10, height=10, left='+screen.width+', top='+screen.height+',toolbar=no,scrollbars=no,resizable,location');
				globalObj.prepop[globalObj.prepopIndex].blur();
				window.focus();
			}
			//--
		},
		getAvailablePopWin: function(){
			var El = this;
			var retVal = new Array();
			for(var x=1;x<globalObj.prepop.length;x++){
				try{
					if(globalObj.prepop[x].location.hostname==''){
						retVal.push(x);
					}
				}catch(e){}
			}
			return retVal;
		},
		getAdsClassification:function(str){
			//debugger;
			for(var x=0;x<this.dt_Ads.length;x++){
				var a = this.dt_Ads[x].advertiser.replace('.com','');
				if(str == 'travelticker') str = 'travel-ticker';
				if(a == str){
					//console.log(str+' - '+this.dt_Ads[x].classification);
					return this.dt_Ads[x].classification;
				}
			}
			//console.log(str);
			return '';
		},
		guidGenerator : function() {
			var S4 = function() {
				return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
			};
			return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
		}
	}
	
	globalObj = Object.create(Z.Global);
	globalObj.init();
});
