/**
Script: idoltracker.class.js
	idoltracker - A javascript class to handle the idol tracker

License:
	MIT-style license.

Copyright:
	Copyright (c) 2009 AmericanIdol.com

Dependencies:
	jQuery 1.2.6: Core
*/

jQuery.noConflict();

function popEdit(state){
	
	//var keeperHeight = document.getElementById('tracker-keeper').offsetHeight;
	var keeperHeight = 800;
	if (state == 'withbg') {
		document.getElementById('edit-bg').style.height = keeperHeight -10 + 'px';
		document.getElementById('edit-bg').style.display = 'block';
		
		document.getElementById('tracker-ad').style.display = 'none';
		//document.getElementById('tracker-container').style.height = keeperHeight;
	}
	document.getElementById('edit-mode').style.height = keeperHeight -90 +'px';
	document.getElementById('edit-mode').style.display = 'block';
	jQuery("#tracker-keeper").css('display', 'block');
	jQuery("#edit-top").css('display', 'block');
}
function unPopEdit(envSwitch){
	document.getElementById('edit-mode').style.display = 'none';
	document.getElementById('edit-bg').style.display = 'none';
	jQuery("#edit-top").css('display', 'none');
	if (jQuery("#tracker-ad").contents() != '') {
		initTracker(envSwitch);
	}
}

var initTracker = function(envSwitch) {
	var settings = _getCookie('myidol_settings');
	
	if (settings == null) {
		// check if they're completely logged out by testing against core_anon cookie
		var anontest = _getCookie('core_anon');
		if (anontest == '1') {
			loadTracker(null, envSwitch);
		} else {
			// try to get the settings
			jQuery.getJSON("/includes/proxy.php?url="+envSwitch+"/myidol/settings/&a=get&s=tracker_contestant&"+Math.random(),
				function (data) {
					if (data != '0') {
						var aContestants = data['settings']['tracker_contestant'];
						loadTracker(aContestants, envSwitch); // load tracker with the settings
					} else {
						loadTracker(null, envSwitch); // fallthrough case - will load ad
					}
				}
			);
		}
		
	} else {
		
		var aContestants = eval( "(" + settings + ")");
		
		if (aContestants['settings']['tracker_contestant'][0] != null) {
			loadTracker(aContestants['settings']['tracker_contestant'], envSwitch);
		} else {
			loadTracker(null, envSwitch);
		}
	}
}

function loadTracker(contestants, envSwitch) {
	if (contestants == null) {
		// check if they're completely logged out by testing against core_anon cookie
		var anontest = _getCookie('core_anon');
		if (anontest != '1') { // if they don't have the core_anon cookie, check their cookies to attemp to determine user id
			// load the ad and assign appropriate click functionality based on login status
			jQuery.getJSON("/includes/proxy.php?url="+envSwitch+"/myidol/checklogin",
				function (data) {
					var loggedIn = data['response'];
					if (loggedIn == 'true') {
						// load the ad
						jQuery("#tracker").load("/includes/proxy.php?url="+envSwitch+"/contestants/idoltracker/",null,function(responseText, textStatus, XMLHttpRequest) { jQuery("#tracker-keeper").css('display', 'none'); jQuery("#tracker-ad").css('display','block'); });
						jQuery("#tracker-ad").empty();
						jQuery("#tracker-ad").prepend('<a style="cursor:pointer;" onclick="popEdit(\'withbg\')"><img src="http://media.americanidol.com/contestants/season_10/tracker/tracker-ad.jpg" width="300" height="300" border="0" /></a>');						
					} else { // this shouldn't happen, but just in case give them the ad
						jQuery("#tracker-ad").prepend('<a href="http://myidol.americanidol.com/tools/quicklogin.one"><img src="http://media.americanidol.com/contestants/season_10/tracker/tracker-ad.jpg" width="300" height="300" border="0" /></a>');
					}
				}
			);
		} else { // they're not logged in; just serve the ad
			jQuery("#tracker-ad").empty();
			jQuery("#tracker-ad").prepend('<a href="http://myidol.americanidol.com/tools/quicklogin.one"><img src="http://media.americanidol.com/contestants/season_10/tracker/tracker-ad.jpg" width="300" height="300" border="0" /></a>');
		}
		
	} else if (contestants[0]){
		// load the first contestant
		jQuery("#tracker").load("/includes/proxy.php?url="+envSwitch+"/contestants/idoltracker/&cid=" + contestants[0]);
	} else if ((typeof contestants) == 'number') {
		// if just a single ID is passed in.
		jQuery("#tracker").load("/includes/proxy.php?url="+envSwitch+"/contestants/idoltracker/&cid=" + contestants);
	} else {
		// error case, in case we got this far with bad data
		loadTracker(null);
	}
}

function changeTracker(id, envSwitch) {
	jQuery("#tracker-keeper").css("display","none");
	jQuery("#spinner-keeper").css("display","block");
	jQuery("#tracker").load("/includes/proxy.php?url="+envSwitch+"/contestants/idoltracker/&cid=" + id);
}
function _getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null;
}

function _inArray(arr,value) {
	var i;
	for (i=0; i < arr.length; i++) {
		if (arr[i] === value) {
			return true;
		}
	}
	return false;
};

function _searchArray(arr,value) {
	var i;
	for (i=0; i < arr.length; i++) {
		if (arr[i] === value) {
			return i;
		}
	}
	return false;
};
