// Wheat Council js

var dakota = { 
	src: 'fonts/dakota.swf', 
	wmode: 'transparent'
};
sIFR.activate(dakota);

sIFR.replace(dakota, {
  selector: 'h2',
  css: '.sIFR-root { color: #780b00; }'
});
sIFR.replace(dakota, {
  selector: 'h3',
  css: '.sIFR-root { color: #780b00; }'
});
sIFR.replace(dakota, {
  	selector: '.numberText',
	css: ['.sIFR-root { color: #ffffff; text-align: right}']
});
function redoSifr(){
	sIFR.replace(dakota, {
		selector: 'h2',
		css: '.sIFR-root { color: #780b00; }'
	});
}

var globalStage; //quick fixes
var globalWheat;

/*
 * Homepage user account functions
 */
function checkAccount(){
	var hash_user = '';
	var hash_code = '';
	getCount();
	if (location.search.length > 1) {
		var hash = location.search.substr(1).split('&');
		hash_user = hash[0].split('=');
		hash_user = hash_user[1].substr(0, 36);
		if (hash.length > 1) {
			hash_code = hash[1].split('=');
			hash_code = hash_code[1];
		}
	}else if (readCookie('user')) {
		hash_user = readCookie('user');
	}else{
		loadDefaultState();
		initMenu();
		return;
	}
	$.ajax({
		type: "POST",
		url: "php/account.php",
		data: "hash_user=" + hash_user +
		"&hash_code=" + hash_code,
		success: function(msg){
			if (msg.indexOf('success') == -1) {
				location.href = 'error.html?' + escape($.trim(msg));	
			}else{
				var msgArray = msg.split(';');
				$('.global .signup').html('<a href="javascript: logout()" title="log out">log out</a>');
				$('.global .login').html('Welcome, ' + msgArray[3] );
				
				initMenu();
				if(msgArray[2] == ''){
					msgArray[2] = -1;
				}
				
			
				
				var stage = Number(msgArray[1]);	
				for(var i = 1; i < 5; i++){
					if(i < stage){
						$(".phases li a:eq("+ Number(i - 1) +")").addClass('avail').attr('href', 'javascript: loadStageState('+ i +','+ msgArray[2] +')');						
					}else if(i == stage){
						$(".phases li a:eq("+ Number(i - 1) +")").addClass('curr').attr('href', 'javascript: loadStageState('+ i +','+ msgArray[2] +')');
					}
				}
				eraseCookie('user');
				eraseCookie('name');
				createCookie('name', msgArray[3], 7);
				createCookie('user', hash_user, 7);
                globalStage = stage;
                globalWheat = msgArray[2];
				loadStageState(stage, msgArray[2]);
			}
		}
	});	
}
function secondaryMenu(){
	if(readCookie('name')){
		var name = readCookie('name');
		$('.global .signup').html('<a href="javascript: logout()" title="log out">log out</a>');
		$('.global .login').html('Welcome, ' + name );
	}
	initMenu();
	getCount();
}
function loadDefaultState(){
	var flashvars = {
		wheat: -1,
		xmlPath: 'XML/phase0.xml',
		stage: -1
	};
	var params = {
		wmode: "transparent"
	};
	var attributes = {};
	attributes.id = 'videoArea';
	swfobject.embedSWF("wheatCouncil.swf", 'videoArea', "866", "458", "9.0.115", "expressInstall.swf", flashvars, params, attributes);
}
function loadStageState(stageVal, wheat){
	for(var i = 1; i < 5; i++){
		if ($(".phases li a:eq(" + Number(i - 1) + ")").hasClass('curr')) {
			$(".phases li a:eq(" + Number(i - 1) + ")").removeClass('curr');
			$(".phases li a:eq(" + Number(i - 1) + ")").addClass('avail');
		}
		if(i == stageVal){
			$(".phases li a:eq("+ Number(i - 1) +")").addClass('curr');
		}
	}
	var xml = 'XML/phase'+ stageVal +'.xml'
	var flashvars = {
		wheat: wheat,
		stage: stageVal,
		xmlPath: xml
	};
	var params = {
		wmode: "transparent"
	};
	var attributes = {};
	attributes.id = 'videoArea';
	swfobject.embedSWF("wheatCouncil.swf", 'videoArea', "866", "458", "9.0.115", "expressInstall.swf", flashvars, params, attributes);

}
function loadPreviewState(stageVal, wheat){
	//stageVal = Number(stageVal) + 1;
	/*for(var i = 0; i < 4; i++){
		if ($(".phases li a:eq(" + i + ")").hasClass('curr')) {
			$(".phases li a:eq(" + i + ")").removeClass('curr');
			$(".phases li a:eq(" + i + ")").addClass('avail');
		}
		if(i == stageVal){
			$(".phases li a:eq("+ i +")").addClass('curr');
		}
	}*/
	initMenu();
	getCount();
	var xml = 'XML/phase'+ stageVal +'.xml'
	var flashvars = {
		wheat: wheat,
		stage: stageVal,
		xmlPath: xml
	};
	var params = {
		wmode: "transparent"
	};
	var attributes = {};
	attributes.id = 'videoArea';
	swfobject.embedSWF("wheatCouncil.swf", 'videoArea', "866", "458", "9.0.115", "expressInstall.swf", flashvars, params, attributes);

}
function logout(){
	eraseCookie('user');
	eraseCookie('name');
	location.href = 'login.html';
}
function initMenu(){
	sIFR.replace(dakota, {
	  selector: '.global li',
	  css: ['.sIFR-root { color: #ffffff; text-align: right}', 
			'a { color: #ffffff; text-decoration: none }',
			'a:hover { color: #7a011d; text-decoration: none }',
			'.active { color: #7a011d; text-decoration: none }'
			]
	});
}

/*
 * Generic cookie functions
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
function getCount(){
	$.ajax({
		type: "GET",
		url: "php/getCount.php",
		success: function(msg){
			$('.numberPeople').html(msg);
			sIFR.replace(dakota, {
			  	selector: '.numberPeople',
				css: ['.sIFR-root { color: #ffffff; text-align: right}']
			});
		}
	});
}
function unsub(){
	var hash_user = '';
	var hash_code = '';

	if (location.search.length > 1) {
		var hash = location.search.substr(1).split('&');
		hash_user = hash[0].split('=');
		hash_user = hash_user[1].substr(0, 36);
		if (hash.length > 1) {
			hash_code = hash[1].split('=');
			hash_code = hash_code[1];
		}
	}else{
		return;
	}
	$.ajax({
		type: "POST",
		url: "php/account.php?unsub",
		data: "hash_user=" + hash_user,
		success: function(msg){
			if (msg.indexOf('success') == -1) {
					
			}else{
				
			}
		}
	});	
}
function nextPhase(){
    if(globalStage < 4){
        $.ajax({
            type: "POST",
            url: "php/autoemail.php",
            success: function(msg){
                if (msg.indexOf('success') != -1) {
                    checkAccount();
                }
            }
        });
    }else{
        location.href = "/quiz.php";
    }
    
}

















