// JavaScript Document
// this swaps the tabs and loads corect first page
function swapTab (name) {
	if (name == 'gym') {
	  document.getElementById('picknmixtopimage').style.backgroundImage = "url(./img/picknmix/gym_selected.gif)";
	  document.getElementById('homeexcersises').style.display = 'none';
      document.getElementById('gymexcersises').style.display = '';
	  //document.getElementById('gym1').style.display = 'block';
		pageno=1;
		for(i=1;i<=window.gym_pages;i++){
				document.getElementById('gym'+i).style.display =(i==pageno)? 'block':'none';
		}

	}
    
	if (name == 'home') {
		document.getElementById('picknmixtopimage').style.backgroundImage = "url(./img/picknmix/home_selected.gif)";
		document.getElementById('gymexcersises').style.display = 'none';
		document.getElementById('homeexcersises').style.display = 'block';
		//document.getElementById('home1').style.display = 'block';
		pageno=1;
		for(i=1;i<=window.home_pages;i++){
				document.getElementById('home'+i).style.display =(i==pageno)? 'block':'none';
		}

	}  
}

function selectPage(pageno, excerciseGroup) {
	if (excerciseGroup == 'gym') {
		for(i=1;i<=window.gym_pages;i++){
				document.getElementById('gym'+i).style.display =(i==pageno)? 'block':'none';
		}
	}
	
	if (excerciseGroup == 'home') {
		for(i=1;i<=window.home_pages;i++){
				document.getElementById('home'+i).style.display =(i==pageno)? 'block':'none';
		}

		
	}
}

function setup() {
	for(i=1;i<=window.home_pages;i++){
	    document.getElementById('home'+i).style.display = 'none';
	}
	for(i=1;i<=window.gym_pages;i++){
	    document.getElementById('gym'+i).style.display = 'none';
	}
	swapTab('gym');
	loadIt();
	update();
//checkForCookie (0);
}



function saveIt(){
	var str = "";
	for(var i=0;i<exercises.length;i++){
		str+= exercises[i].on+",";
	}
	setCookiepick("axahomeex",str,60);
}

function loadIt(){
	var str = getCookie("axahomeex");
	var nums = str.split(",");
	for(var i=0;i<exercises.length;i++){
		exercises[i].on = Number(nums[i]);
		if (exercises[i].on == 1){
		document.getElementById('button'+(i+1)).style.backgroundImage = "url(./img/picknmix/added.gif)"; 
		document.getElementById('button'+(i+1)).innerHTML = '<span id="text'+i+'" class="clcickbutton"> This has been added</span>';
		}
		if (exercises[i].on == 0) {
			document.getElementById('addedExcercises').style.display = 'block';
			document.getElementById('button'+(i+1)).style.backgroundImage = "url(./img/picknmix/add_my_program.gif)"; 
		    document.getElementById('button'+(i+1)).innerHTML = '<a id="buttonLink_'+(i+1)+'" title="Add to my program" onclick="changeButton(this); return false;" href="javascript:void(0);"><span id="text'+(i+1)+'" class="clickbutton">Add to <strong> My Program </strong></span></a>';
		}
	}
}

function update(){
	var count = 0; 
	for(var i=0;i<exercises.length;i++){
		var lnk = document.getElementById("exlink_"+(i+1));
		if(lnk){
			document.getElementById('listex_'+(i+1)).style.display = (exercises[i].on)?"block":"none";
			if (exercises[i].on == 1){
			count += 1;
			} else {
				document.getElementById('button'+(i+1)).style.backgroundImage = "url(./img/picknmix/add_my_program.gif)"; 
		        document.getElementById('button'+(i+1)).innerHTML = '<a id="buttonLink_'+(i+1)+'" title="Add to my program" onclick="changeButton(this); return false;" href="javascript:void(0);"><span id="text'+(i+1)+'" class="clickbutton">Add to <strong> My Program </strong></span></a>';
			}
		}
		if (count == 0) {
			document.getElementById('addedExcercises').style.display = 'none'; 
		} else {
			document.getElementById('addedExcercises').style.display = 'block';
		}
	}
	
	
	var tots = calculateTotals();
	var totcal = document.getElementById("totcal");
	if(totcal) totcal.innerHTML = tots.cal;
		
	var tottim = document.getElementById("tottim");
	if(tottim) tottim.innerHTML = tots.time;
	
	var totex = document.getElementById("totex");
	if (totex) totex.innerHTML = tots.tex;
}
function calculateTotals(){
	var totcal = 0;
	var tottim = 0;
	var totex  = 0;
	for(var i=0;i<exercises.length;i++){
		if(exercises[i].on){
			totcal+=exercises[i].cal;
			tottim+=exercises[i].time;
			totex+=1;
		}
	}
	
	return {cal:totcal,time:tottim,tex:totex};
}

function toggleExercise(o){

	var i = Number(o.attributes['id'].value.replace(/exlink_/,""));
	if(!exercises[i-1].on){
		// ADD
		exercises[i-1].on = 1;
	} else {
		// REMOVE
		exercises[i-1].on = 0;
	}
	update();
	saveIt();
}

/* this calls the ajax when you click on the button  */
function changeButton(o) 
{               
	var i = Number(o.attributes['id'].value.replace(/buttonLink_/,""));
	if(!exercises[i-1].on){
		// ADD
		exercises[i-1].on = 1;
		document.getElementById('button'+i).style.backgroundImage = "url(./img/picknmix/added.gif)"; 
		document.getElementById('button'+i).innerHTML = '<span id="text'+i+'" class="clcickbutton"> This has been added</span>';
		//document.getElementById('text'+i).innerHTML = 'This has been added';
	} else {
		// REMOVE
		exercises[i-1].on = 0;
		document.getElementById('button'+i).style.backgroundImage = "url(./img/picknmix/add_my_program.gif)"; 
		document.getElementById('text'+(i+1)).innerHTML = '<a id="buttonLink_'+i+'" title="Add to my program" onclick="changeButton(this); return false;" href="javascript:void(0);"><span id="text'+i+'" class="clickbutton">Add to <strong> My Program </strong></span></a>';
	}
	update();
	saveIt();
}

/* this removes the excercise from the my program page  */
function RemoveButton(o) 
{               
	var i = Number(o.attributes['id'].value.replace(/remove_/,""));

	if(!exercises[i-1].on){
		// ADD
		exercises[i-1].on = 1;
	} else {
		// REMOVE
		exercises[i-1].on = 0;
	}
	saveIt();
	var tots = calculateTotals();
	var totcal = document.getElementById("totcal");
	if(totcal) totcal.innerHTML = tots.cal;
		
	var tottim = document.getElementById("tottim");
	if(tottim) tottim.innerHTML = tots.time;
	document.getElementById('excercisechoice_'+i).style.display = 'none'; 
}


/*  this sets a cookie   */
function setCookiepick(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

/* this gets a cookie    */
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 "";
}

var total_records=20;
function getPaging(type,pageno){
	pages=Math.round(total_records/4);
	var output='<ul class="pagestop">';
	for(i=1;i<=pages;i++){
		if(i==pageno){
			output+='<li>'+i+' |</li>';
		}else{
			output+='<li><a href="javascript:void(0);" onclick="selectPage('+i+', \''+type+'\'); return false;">'+i+'</a> | </li>';
		}
	}
	if(pageno!=total_records)output+='<li><a href="javascript:void(0);" onclick="selectPage('+(pageno+1)+', \''+type+'\'); return false;"> Next &raquo;</a> </li>';
	output+='</ul>';
	document.getElementById(type+'_paging_'+pageno).innerHTML=output;

}
