jQuery.fn.exists = function(){return jQuery(this).length>0;}

function trans(lang){
	var date = new Date();
	date.setTime(date.getTime() + (1 * 23 * 60 * 60 * 1000));
	$.cookie('language',  lang, { expires: date, path: '/' });
	if(lang=='en'){ 
		top.location.href = location.href; 
	} 
	$('body').translate(lang,{not: '#header h1, #map'});
}

function showPop(c_value){
	if(!$.colorbox.element().length){  
		$.colorbox({href:"/request/"+ popup_offer +"/?from=colorbox",opacity:0.7});
		c_value++;
		$.cookie('popup_count',  c_value, { expires: 365, path: '/'  });	
	}
}

//////////////////////////////////////////////////////////
// jQuery - Document Ready Function

$(document).ready(function() { 

	//////////////////////////////////////////////////////////
	// Enable Superfish Dropdown Menus
	$('ul.sf-menu').superfish( { animation: {opacity:'show'}, autoArrows: false, speed:200, delay: 650 } );

	//////////////////////////////////////////////////////////
	// Flow Player Videos
	
	if ($("#main-column #flowplayer").exists()) {

		// Old method, doesn't work with iPad plugin - JCM 08-26-11
		// $("#main .flowplayer").flowplayer({
		//  	src:"/javascripts/flowplayer/flowplayer-3.2.7.swf", 
		//  	wmode: 'transparent'
		// }).ipad();

		// if clip is configured to not autoPlay, then iPad/iPhone will not autoPlay
		flowplayer("flowplayer", {src: "/javascripts/flowplayer/flowplayer-3.2.7.swf", wmode: 'transparent'}, {
			key:'false',
			clip: {
				autoPlay: true, 
				autoBuffering: true
			}
		}).ipad();

	}

	if ($("#main-column #flowplayer2").exists()) {
		// use id="flowplayer2" for 2nd video on same page
		flowplayer("flowplayer2", {src: "/javascripts/flowplayer/flowplayer-3.2.7.swf", wmode: 'transparent'}, {
			key:'false',
			clip: {
				autoPlay: false, 
				autoBuffering: true
			}
		}).ipad();
	}

	if ($(".flowplayer").exists()) {
		fp = $(".flowplayer").flowplayer(0).ipad();				
	} 

	if (popup_offer != 'disabled' && parseInt($.cookie('invest_downloaded')) != 1 && window.location.pathname.indexOf("invest") > -1) {
		$.doTimeout(8000, function() {
			if (!$.colorbox.element().length) {  // Detect if colorbox is already open!  
				$.colorbox({href:"/request/investor_guide?from=colorbox",opacity:0.7});	
			}
		});	
	} else {

		// Reset cookies. TODO: Disable reset.
		// $.cookie('relo_days ',  null, { expires: 0, path: '/',  });
		// $.cookie('relo_count',  null, { expires: 0, path: '/',  });
		c_value = parseInt($.cookie('popup_count')) || 0;

		if (popup_offer != 'disabled' && $.cookie('relo_downloaded') != '1' && $.cookie('popup_days') != '1' && c_value < 4) {

			$.doTimeout(30000, function(){
						
				var date = new Date();
				date.setTime(date.getTime() + (1 * 23 * 60 * 60 * 1000));
				$.cookie('popup_days',  '1', { expires: date, path: '/' });

				if (!$(".flowplayer").exists() || fp == null || fp.isLoaded() == false) {	
					showPop(c_value); 
				} else if(fp.getState() >3 || fp.getState() == 1){ 
					showPop(c_value);
				} else {					 		
					fp.onFinish(function() {	$.doTimeout(3000, function(){	showPop(c_value);	});		});
					fp.onPause(function()  {	$.doTimeout(5000, function(){	if(fp.getState() > 3){	showPop(c_value);	}		});		});
				}
			});
		}
	}
	
	//////////////////////////////////////////////////////////
	// Colorbox Links
	$('a.email').colorbox({opacity:0.7,from:'colorbox'});
	$('.colorbox').colorbox({opacity:0.7,from:'colorbox'});
	
	if($.cookie('language') != null & $.cookie('language') != 'en' ){
		trans($.cookie('language'))
	}
			
	// sourceText = encodeURIComponent($('.quick-search h2').text());
	// function saveLang(text){
	//		lang = text.language;
	//	}
	//	if(google != null && google.language != undefined){
	//		google.language.detect(sourceText, saveLang);
	//	}
}); 


