var product_pics = new Array();

var cur_tab = 0;

function setTab(tabid){
	cur_tab = tabid;
}

function swapTab(tabid){
	
	if(cur_tab != 0){
		
		var cur_elem = document.getElementById(cur_tab + "_tab");
		
		if(cur_elem){
			cur_elem.setAttribute("class", "tab_off");
			cur_elem.setAttribute("className", "tab_off");
		}
		
		toggleElement(cur_tab, 0);
	}
	
	var elem = document.getElementById(tabid + "_tab");
		
	if(elem){
		elem.setAttribute("class", "tab_on");
		elem.setAttribute("className", "tab_on");
	}
	toggleElement(tabid, 1);
	cur_tab = tabid;
		
}

function changeFont(size){

//	var p = document.getElementsByTagName('div');
//    for(i=0;i<p.length;i++) {
//       if(p[i].style.fontSize) {
//          var s = parseInt(p[i].style.fontSize.replace("px",""));
//       } else {
//          var s = 11;
//       }
//       
//       s = s + size;
//       
//       if(s > 13){
//       		s = 13;
//       }
//       
//       if(s < 11){
//       		s = 11;
//       }
//       
//       p[i].style.fontSize = s+"px"
//    }
	
	var p = document.getElementById('prod_descrip');
	if(p.style.fontSize){
		 var s = parseInt(p.style.fontSize.replace("px",""));	
	}else {
         var s = 11;
	}
	
	s = s + size;

	if(s > 13){
       	s = 13;
	}
	
	if(s < 11){
		s = 11;
	}
	
	p.style.fontSize = s+"px"
	
}

function toggleElement(id, onoff){
	
	var elem = document.getElementById(id);
	
	if(elem){
		
		if(onoff == 0){
			elem.style.display = "none";
		}else{
			elem.style.display = "block";
		}
		
	}
	
}

function lbGetObject(id){
	var object = null;
	
	if ( document.getElementById ){
		object = document.getElementById(id);
	}else if ( document.all ){
		object = document.all[id];
	}
	
	return(object);
}

function lbElementGetX(elem){
	
	var xpos = elem.offsetLeft;
	var tempe = elem.offsetParent;
	while (tempe != null){
    	xpos += tempe.offsetLeft;
       	tempe = tempe.offsetParent;
    }
    
    return(xpos); 
	
}

function lbElementGetY(elem){
	
	var ypos = elem.offsetTop;
	var tempe = elem.offsetParent;
	while (tempe != null){
    	ypos += tempe.offsetTop;
       	tempe = tempe.offsetParent;
    }
    
    return(ypos); 
	
}

function lbGetX(id){
	
	var elem = lbGetObject(id);
	var xpos = elem.offsetLeft;
	var tempe = elem.offsetParent;
	while (tempe != null){
    	xpos += tempe.offsetLeft;
       	tempe = tempe.offsetParent;
    }
    
    return(xpos); 
	
}

function lbGetY(id){
	
	var elem = lbGetObject(id);
	var ypos = elem.offsetTop;
	var tempe = elem.offsetParent;
	while (tempe != null){
    	ypos += tempe.offsetTop;
       	tempe = tempe.offsetParent;
    }
    
    return(ypos); 
	
}


function imageZoom(path, img){
	var loc = img.replace("#", "%23");
	window.open(path+"image_zoom.php?img="+loc+"", "image_zoom", "status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=yes, height=540, width=540");
}

function lbPopUp(url, width, height){
	window.open(url, "lb_pop", "status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=yes, height="+height+", width="+width+"");
}

function changeProductPic(elem){
	if(elem){
		
		if(elem.type == "radio"){
			var key = elem.value;
			selectProductPic(document.getElementById('product_main_pic'), elem.name+"_"+key);	
		}else{
		
			var select = elem.selectedIndex;
			
			
			if(select != -1){
				var key = elem.options[select].value;
				selectProductPic(document.getElementById('product_main_pic'), elem.name+"_"+key);
			}
		}
	}
}

function selectProductPic(elem, key){
	var pic = getProductPic(key);
	
	if(pic && elem){
		elem.src = LB_BASE_URL + "lb_image_prep.php?imagepath=" + pic + "&slug=_preview";
		elem.onclick = function(){imageZoom(''+LB_BASE_URL+'', ''+LB_BASE_URL+'image_zoom'+pic.replace(/product_pics/, '')+'')};
	}
}

function getProductPic(key){
	return(product_pics[key]);
}

function addProductPic(key, pic){
	product_pics[key] = pic;
}

function uploadedFile(file){
	window.open("uploaded_file.php?file="+file+"", "uploaded", "status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=yes, height=540, width=540");	
}

function imageZoomOption(path, id){
	
	var elem = document.getElementById('inner_pic');
	if(elem){
		var img = elem.src;
		if(img){
			imageZoom(path, img);
		}
	}
}

var chars = new Array();
function loadCharity(charityid, charity_text){
	chars[charityid] = charity_text;
}

function displayCharity(elem, elem2){
	
	if(elem){
		if(elem.selectIndex != -1){
			opt = elem.options[elem.selectedIndex].value;
			if(opt != 0){
				if(elem2){
					elem2.innerHTML = chars[opt];
				}
			}else{
				if(elem2){
					elem2.innerHTML = "";
				}
			}
		}
	}
}

var personal = new Array();
var count = 0;
function loadPersonal(personalizedid){
	personal[count] = personalizedid;
	count++;
}

function displayPersonalized(personalizedid){
	for(i=0; i<personal.length; i++){
		if(personal[i] == personalizedid){
			toggleElement("input_"+personal[i], 1);
		}else{
			toggleElement("input_"+personal[i], 0);
		}
	}
	
}

function selectRegistry(dockElement){
	
	var ajax_call = new LB_Ajax();
	ajax_call.getPage(LB_BASE_URL + 'ajax_index.html?page=gift_registry', function(responseText) {
		
		if(responseText == "0"){
			// no registry created, redirect to create registry
			window.location.href = LB_BASE_URL + 'member/create_registry.html?registryid=0';
			return false;
		}
		
		var registryid = parseInt(responseText);
		
		if(!isNaN(registryid)){
			// only one registry, lets use it
			selectedRegistry(registryid);
		}else{
			// multiple registries, lets select
			var obj = lbGetObject('registry_selection');
			if(obj){
				var x = lbElementGetX(dockElement);
				var y = lbElementGetY(dockElement);
				obj.style.top = y + "px";
				obj.style.left = x + "px";
				obj.style.display = 'block';
				var obj2 = lbGetObject('registry_selection_items');
				if(obj2){
					obj2.innerHTML = responseText;
				}
			}
		}
		
	});
	
}

function selectedRegistry(registryid){
	
	var obj = lbGetObject('registryid');
	
	if(obj){
		obj.value = registryid;
		var form = lbGetObject('add_form');
		form.submit();
	}
}

function closeRegistry(){
	
	var obj = lbGetObject('registry_selection');
	if(obj){
		obj.style.display = 'none';
	}
	
}

function selectReminder(dockElement){
	
	var ajax_call = new LB_Ajax();
	ajax_call.getPage(LB_BASE_URL + 'ajax_index.html?page=gift_reminder', function(responseText) {
		
		if(responseText == "0"){
			// no registry created, redirect to create registry
			window.location.href = LB_BASE_URL + 'member/edit_gift_reminder.html?reminder=0&return_productid='+LB_SAVED_PRODUCTID;
			return false;
		}
		
		var reminderid = parseInt(responseText);
			
		
		if(!isNaN(reminderid)){
			// only one reminder, lets use it
			selectedReminder(reminderid);
		}else{
			// multiple registries, lets select
			var obj = lbGetObject('reminder_selection');
			if(obj){
				var x = lbElementGetX(dockElement);
				var y = lbElementGetY(dockElement);
				obj.style.top = y + "px";
				obj.style.left = x + "px";
				obj.style.display = 'block';
				var obj2 = lbGetObject('reminder_selection_items');
				if(obj2){
					obj2.innerHTML = responseText;
				}
			}
		}
		
	});
	
}

function selectedReminder(reminderid){
	
	var obj = lbGetObject('reminderid');
	
	if(obj){
		obj.value = reminderid;
		var form = lbGetObject('add_form');
		form.submit();
	}
}

function closeReminder(){
	
	var obj = lbGetObject('reminder_selection');
	if(obj){
		obj.style.display = 'none';
	}
	
}

function swapImages(location, zoom_location){
	
	//var swap = lbGetObject('product_main_pic_holder');
	var swap = lbGetObject('product_main_pic');
	
	if(swap){
		//swap.innerHTML = '<img src="'+location+'" width="247" height="247" id="product_main_pic" onclick="imageZoom(\''+LB_BASE_URL+'\', \''+location+'\');" style="cursor:pointer;" />';
		
		
		swap.src = location.replace("#", "%23");
		if(zoom_location){
			swap.onclick = function(){imageZoom(''+LB_BASE_URL+'', ''+zoom_location+'')};
		}else{
			swap.onclick = function(){imageZoom(''+LB_BASE_URL+'', ''+location+'')};
		}			
		
		
		//alert(swap.src);
		//swap.onclick = function(){imageZoom(''+LB_BASE_URL+'', ''+location+'')};
	}
	
}

var personS = new Array();

function addPersonS(index, val){
	personS[index] = val;
}

function handlePerson(elem){
	if(elem){
		showPerson(elem.selectedIndex);
	}
}

function showPerson(index){
	var elem = document.getElementById('style_preview');
	
	if(elem){
		elem.src = personS[index];
	}
}

function emailSite(){
	
	lbPopUp(""+LB_BASE_URL+"email_site.php", '400', '450');
	
}

var sliderID = null;
var slide = 0;

function startSlide(amount){
	
	//slide = 1;
	
	//while(slide == "1"){
	//	window.setTimeout('shoppingCartSlide(\''+amount+'\')', 1000);
	//}
	
	
    sliderID = setInterval('shoppingCartSlide(\''+amount+'\')',30);
	
}

function endSlide(){
	slide = 0;
	clearInterval(sliderID);
}

function shoppingCartSlide(amount){
	
	var elem = document.getElementById('top_shopping_cart_item_holder');
	
	var cur_x = elem.style.left.replace("px","");
	
	
	if(!cur_x){
		cur_x = 0;	
	}
	
	cur_x = parseInt(cur_x) + parseInt(amount);
	
	
	elem.style.left = cur_x + "px";
		
}


function swapClasses(elem, class_name){
	
	//var elem = document.getElementById(id);
	if(elem){
		elem.className = class_name;
	}
	
}

function addClass(elem, class_name){
	
	//var elem = document.getElementById(id);
	if(elem){
		var cur_class = elem.className;
		elem.className = cur_class = " " + class_name;
	}
	
}

function removeClass(elem, class_name){
	
	//var elem = document.getElementById(id);
	if(elem){
		var cur_class = elem.className;
		elem.className = cur_class.replace(class_name, "");
	}
	
}

function replaceClass(elem, old_class_name, new_class_name) {
	removeClass(elem, old_class_name);
	addClasS(elem, new_class_name);	
}

function removeFromCart(itemid){
	
	new Ajax.Request(
			LB_BASE_URL + 'ajax_index.html', 
			{
				method: 'post', 
				parameters: 'itemid='+itemid+'&page=remove_cart', 
				onComplete: updateShoppingCart,
				asynchronous: true
			});
	
}

function updateShoppingCart(ajax){
	
	if(ajax.readyState == 4 && ajax.status == 200)
	{
		document.getElementById('shopping_cart_ajax').innerHTML = ajax.responseText;
		
		new Ajax.Request(
			LB_BASE_URL + 'ajax_index.html', 
			{
				method: 'post', 
				parameters: 'page=get_cart_total', 
				onComplete: updateShoppingCartTotal,
				asynchronous: true
			});
		
	}
	
}

function updateShoppingCartTotal(ajax){
	
	if(ajax.readyState == 4 && ajax.status == 200)
	{
		document.getElementById('shopping_cart_total_price').innerHTML = ajax.responseText;
		
	}
	
}

function lbLaunchLiveChat(){
	
	window.open(LB_BASE_URL + "live_help.php", "lb_live_chat", "status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=0, height=300, width=400");
	
}

function setPlayerPos(playerid, pos){
	
}

function setPlayer(container, id){
//	var elem = document.getElementById(container);
//	if(elem){
//		elem.innerHTML = document.getElementById(id).parent.innerHTML;
//	}
//	alert(id);
}

function countDown(seconds, teamid, redirect){
	var days = Math.floor(seconds / (86400));
	var hours = Math.floor(seconds / (3600)) - (days * 24);
	var mins = Math.floor(seconds / (60)) - (hours * 60) - (days * 24 * 60);
	var secs = Math.floor(seconds) - (mins * 60) - (hours * 60 * 60) - (days * 24 * 60 * 60);
	
	var elem = document.getElementById('lb_count_down');
	
	if(elem){
		elem.innerHTML = days + " days, " + hours + " hours, " + mins + " minutes, " + secs + " seconds";
		var new_seconds = seconds - 1;
		if(new_seconds <= 0 && redirect == 1){
			window.location.href = "/Draft.html?teamid="+teamid;	
		}else{
			if(new_seconds <= 0){
				elem.innerHTML = " 0 days, 0 hours, 0 minutes, 0 seconds";
			}else{
				setTimeout("countDown(" + new_seconds + ", '" + teamid + "', " + redirect + ")", 1000);
			}
		}
	}
	
	
	
	//alert(days + " days, " + hours + " hours, " + mins + " minutes, " + secs + " seconds");
}

function ajaxSchedule(week, leagueid){
	
	document.getElementById('league_schedule').innerHTML = "<div style=\"padding-top:20px; padding-left:10px; font-size:11px; text-align:center; color:#9c9c9c;\"><img src=\"images/ajax-loader.gif\" /><div style=\"padding-top:4px;\">LOADING...</div></div>";
	
	new Ajax.Request('/ajax_index.php?page=ajax_getLeagueSchedule&week='+week+'&leagueid='+leagueid+'',
	  {
	    method:'POST',
	      onSuccess: function(transport){
	      		
		      var response = transport.responseText;
		      	
		      document.getElementById('league_schedule').innerHTML = response;	      
		      
	      
	    }
	  });
	
}