if(typeof console == 'undefined') console = {};

//setup the plugin when using chrome
//use global os_browser
if(os_browser=='winchrom'){
	setTimeout(function(){
		jQuery.ajaxSetup( {
			xhr: function(){
				try{
					return window.kikin.plugin.createXmlHttpRequest();
				}catch(e){
					return new window.XMLHttpRequest();
				}
			}
		});
	},1000);
}

if((window.location.protocol + "//" + window.location.host + "/") == window.location.href){
	Cufon.replace('#header-slogan, .ctastrip-cta-text, .ctastrip-testimonial-quote, .ctastrip-testimonial-cite, .blog-syndication, .vagFont', {
		fontFamily: 'vag',
		forceHitArea: true,
		hover: true
	});	
}else{
	Cufon.replace('#header-slogan, .ctastrip-cta-text, .ctastrip-testimonial-quote, .ctastrip-testimonial-cite, .blog-syndication a, h2, h3, .vagFont', {
		fontFamily: 'vag',
		forceHitArea: true,
		hover: true
	});
}

function disableClick(event) {
    event.preventDefault();
	jQuery('.download-cta-body form').addClass('click-verify');
    return false;
}

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	var result = (isIE) ? window[movieName] : document[movieName];
	if(result) {
		return result;
	} else {
		return false;
	}
}

var Homepage={
	Download:function(){
		KikinFlashInterface.Download();
		this.StopVideo();
	},
	
	StopVideo:function(){
		var oVideo = getFlashMovie('flash-video');
		if(oVideo && oVideo.StopVideo){
			oVideo.StopVideo();
		}
	},
	
	_convertFrames:[],
	CreateConversionIframe:function(_src){
		if(0==jQuery('#kknTrkFrame').length){
			var dvFrameCont = document.createElement('div');
			dvFrameCont.innerHTML='<iframe allowtransparency="true"\
										src="about:blank"\
										id="kknTrkFrame"\
										name="kknTrkFrame"\
										bgcolor="transparent"\
										frameborder="0"\
										style="background:transparent;position:absolute;top:0px;left:0px;width:10px;height:10px;">\
									</frame>';
			document.body.appendChild(dvFrameCont);
		}
		
		if(this._convertFrames.join(',').indexOf(_src)==-1){
			jQuery('#kknTrkFrame').attr('src','/site/'+_src);
			this._convertFrames.push(_src);
		}
	}
};

var KikinLibIntereface={
		
	showSettings:function(){
		if(window.Kikin)
			window.Kikin.ExternalInterface.SettingsWindow.show();
	},
	showShareKikin:function(){
		if(window.Kikin) 
			Kikin.ExternalInterface.ShareKikin();
	}
		
}

var KikinFlashInterface={
	Download:function(){
		//only track downloads not re-installs
		if(bInstalled=='false'){
			trackInstallClick('flash_button');
		}
		
		var dlURL;
		switch(os_browser){
			case "winie":
				if(navigator.userAgent.indexOf('MSIE 6')>-1 || navigator.userAgent.indexOf('MSIE 5')>-1){
					this.UnsupportedBrowser();
				}else{
					dlURL='http://www.kikin.com/download/kikin/KikinInstallerWin.exe';
				}
			break;
//			case "macsaf":
//				if(navigator.userAgent.indexOf('PPC Mac OS X')==-1){
//					dlURL='http://www.kikin.com/download/kikin/KikinInstallerMacSafari.zip';
//				}
//			break;
			case "winsaf":
				alert('Sorry, kikin is not available for Safari on the PC. Please try kikin for Firefox.');
			break;
			case 'macchrom':
				alert('Sorry, but kikin for Chrome is not available.  Please try kikin with Firefox');
			break;
			case 'winchrom':
			case 'macmoz':
			case 'winmoz':
				if(navigator.userAgent.indexOf('Firefox/2.')>-1 || navigator.userAgent.indexOf('Firefox/1.')>-1){
					this.UnsupportedBrowser();
				}else{
					tb_show(null,'#TB_inline?height=500&width=607&inlineId=download-cta-content');
				}
			break;
			default:
				this.UnsupportedBrowser();
			break;
		}
		if(null!=dlURL){
			if(os_browser!='winie'){
				setTimeout(function(){
					window.location=dlURL;
				},200);
			}else{
				window.location=dlURL;
			}
		}
	},
	GotoUninstallPage:function(){
		var hash="#",
		userAgent=navigator.userAgent;
		if(/Firefox/.test(userAgent)){
			hash+="Firefox";
		}else if(/Safari/.test(userAgent)){
			hash+="Safari";
		}else if(/MSIE/.test(userAgent)){
			hash+="InternetExplorer";
		}
		
		trackKikinClick('bottombar','uninstall','installed');
		
		setTimeout(function(){
			window.location="/site/uninstall/browser"+hash;
		},100);
	},
	UnsupportedBrowser:function(){
		alert('Sorry, your browser is not supported, please switch\nto the latest version of Firefox or Internet Explorer');
	}
};

function welcomeClick(){
	if(window.pageTracker){
		pageTracker._trackPageview("/welcome_funnel/button.html");
	}
	setTimeout(function(){
		window.location='http://www.google.com/search?q=coldplay';
	},250);
}

function trackInstallClick(_step){
	var pv;
	switch(_step){
		case 'home':
			pv="/install_funnel/home.html";
		break;
		case 'download_button':
		case 'flash_button_b':
		case 'flash_button':
			pv="/install_funnel/button.html";
		break;
		case 'firefox_checkbox':
			pv="/install_funnel/firefox_checkbox.html";
		break;
		case 'firefox_button':
			pv="/install_funnel/firefox_accept.html";
		break;
		case 'reinstall':
			pv="/install_funnel/reinstall.html";
		break;
	}
	if(null!=pv && window.pageTracker)
		pageTracker._trackPageview(pv+'?os_browser='+os_browser);
}
function gotoUninstallPage(){
	KikinFlashInterface.GotoUninstallPage();
}
function showKikin(){
	KikinLibIntereface.showSettings();
	trackKikinClick('bottombar','settings','installed');
};

function shareKikin(){
	KikinLibIntereface.showShareKikin();
	trackKikinClick('bottombar','share','installed');
};

function trackKikinClick(_cat,_act,_opt_lbl,_opt_val){
	try{
		pageTracker._trackEvent(_cat,_act,_opt_lbl,_opt_val);
	}catch(e){}
}

jQuery(document).ready(function(){

	//Language selector
	jQuery('#language-selector ul').hide();
	jQuery('#language-selector #language-selector-button').click(function(event){
		event.preventDefault();
		jQuery('#language-selector ul').stop().toggle('fast');
	});
	
	//Modal download dialog
	var btn = jQuery('.download-cta-body .download-cta-body-download, .download-cta-body-download-text');

	var el = jQuery('.download-cta-body form input'); //IE doesn't accept the checkbox ID.
	btn.bind('click', function(evt){
		if(!el.attr('checked')){
			disableClick(evt);
		}
	}).addClass('download-disabled');
	
	el.click(function(){
		if(jQuery(this).attr('checked')) {
			btn.removeClass('download-disabled');
		} else {
			btn.addClass('download-disabled');
		}
	});
		
});
