// JavaScript Document
function el(ObjID) {
	return document.getElementById(ObjID);	
}

var temp;
var activeRecommendedProduct = 0;
function slideRecommendedProducts(ProductID,ObjID) {
	jQuery.post("/Resources/Php/getRecommendedProducts.php", {ProductID:ProductID,ObjID:ObjID},
		function(data) {
			if(typeof data.returns == 'string') {
				var pos = jQuery("#" + ObjID + "-" + ProductID).position();
				var width = jQuery("#" + ObjID + "-" + ProductID).width();
				jQuery("#" + ObjID + "-"+ ProductID).after(data.returns);
				jQuery("#" + ObjID + "-" + data.ProductID).animate({"left": -width + "px"},500,"swing");
				jQuery("#" + ObjID + "-" + ProductID).animate({"left": -width + "px"},500,"swing",function callback() {																					
					jQuery("#" + ObjID + "-" + ProductID).remove();
					jQuery("#" + ObjID + "-" + data.ProductID).css("left","0px");
					temp = setTimeout("slideRecommendedProducts(" + data.ProductID + ",\"" + ObjID + "\")",10000);
					activeRecommendedProduct = data.ProductID;
				});
			}
		},"json"
	);
}

function stopSlideRecommendedProducts() {
	clearTimeout(temp);
	temp = null;
}

function startSlideRecommendedProducts() {
	if(temp == null)
		temp = setTimeout("slideRecommendedProducts(" + activeRecommendedProduct + ",\"recomandam-slide\")",10000);	
}

var tempSup;
var activeProductSupplier = 0;
function slideProductSuppliers(ProductSupplierID,ObjID) {
	jQuery.post("/Resources/Php/getProductSuppliers.php", {ProductSupplierID:ProductSupplierID,ObjID:ObjID},
		function(data) {
			if(typeof data.returns == 'string') {
				var pos = jQuery("#" + ObjID + "-" + ProductSupplierID).position();
				var width = jQuery("#" + ObjID + "-" + ProductSupplierID).width();
				jQuery("#" + ObjID + "-"+ ProductSupplierID).after(data.returns);
				jQuery("#" + ObjID + "-" + data.ProductSupplierID).animate({"left": -width + "px"},500,"swing");
				jQuery("#" + ObjID + "-" + ProductSupplierID).animate({"left": -width + "px"},500,"swing",function callback() {																					
					jQuery("#" + ObjID + "-" + ProductSupplierID).remove();
					jQuery("#" + ObjID + "-" + data.ProductSupplierID).css("left","0px");
					tempSup = setTimeout("slideProductSuppliers(" + data.ProductSupplierID + ",\"" + ObjID + "\")",10000);
					activeProductSupplier = data.ProductSupplierID;
				});
			}
		},"json"
	);
}

function stopSlideProductSuppliers() {
	clearTimeout(tempSup);
	tempSup = null;
}

function startSlideProductSuppliers() {
	if(tempSup == null)
		tempSup = setTimeout("slideProductSuppliers(" + activeProductSupplier + ",\"product-suppliers-slide\")",10000);	
}

var openedCategory = null;
function slide(ObjID) {
	openedCategory != ObjID ? slide(openedCategory) : openedCategory = null;
		
	if(jQuery("#"+ObjID).css("display") == 'none') {
		jQuery("#"+ObjID).slideDown(600, function callback() {
			jQuery("#"+ObjID+"-image").attr("src","/Resources/Images/minus.png");										  
		});
		openedCategory = ObjID;
	} else {
		jQuery("#"+ObjID).slideUp(600, function callback(){
			jQuery("#"+ObjID+"-image").attr("src","/Resources/Images/plus.png");											
		});
	}
}

var openedProduct = null;
function slideProduct(ObjID) {
	openedProduct != ObjID ? slideProduct(openedProduct) : openedProduct = null;
		
	if($("#"+ObjID).css("display") == 'none') {
		$("#"+ObjID).slideDown(300);
		openedProduct = ObjID;
	} else {
		$("#"+ObjID).slideUp(300);
	}
}

function changePhoto(ObjID,NewPhotoSrc) {
	jQuery("#"+ObjID+"Link").attr("href","/Resources/Upload/Photos/"+NewPhotoSrc);
	jQuery("#"+ObjID).attr("src","/Resources/Upload/Photos/VeryBigThumbs/"+NewPhotoSrc);
	return true;
}

function fillStars(number,type) {
	for(var i=1; i <= number; i++) {
		jQuery("#star-"+i).attr("src","/Resources/Images/"+type+"-star.png");	
	}	
}

function fillSpecifications(ObjID,ProductFeatureID) {
	jQuery("#"+ObjID).find("option").remove();
	jQuery.post("/Resources/Php/getSpecifications.php", {ProductFeatureID:ProductFeatureID},
		function(data) {
			if(data.productFeatureID != '') {
				jQuery("#"+ObjID).append('<option value="">------</option>');
				var productFeatureID = data.ProductFeatureID.split(",");
				var productFeatureName = data.ProductFeatureName.split(",");
				for(var i=0; i < productFeatureID.length; i++) {
					jQuery("#"+ObjID).append('<option value="'+productFeatureID[i]+'">'+productFeatureName[i]+'</option>');
				}
			}
		},"json"
	);
}

function submitSpecifications(URL,ProductFeatureParent,ProductFeatureID) {
	var specifications = '';
	jQuery("select[name='"+ProductFeatureID+"']").each(function(i){
		jQuery(this).addClass(ProductFeatureID+i);
	});
	
	jQuery("select[name='"+ProductFeatureParent+"']").each(function(i){
		if(jQuery("."+ProductFeatureID+i).attr("value") != '')
			specifications += jQuery(this).attr("value")+'-'+jQuery("."+ProductFeatureID+i).attr("value")+',';
	});
	specifications = specifications.substr(0,specifications.length-1);
	specifications = specifications != '' ? specifications : '-'; 
	
	document.location = URL.replace("{variable}",specifications);
}			

function fillCounties(CountryID,ObjID) {
	jQuery("#"+ObjID).find("option").remove();
	jQuery.post("/Resources/Php/getCounties.php", {CountryID:CountryID},
		function(data) {
			if(data.returns != '') {
				jQuery("#"+ObjID).append('<option value="">------</option>');
				var county;
				var countries = data.returns.split(",");
				for(var i=0; i < countries.length; i++) {
					county = countries[i].split("-");
					jQuery("#"+ObjID).append('<option value="'+county[0]+'">'+county[1]+'</option>');
				}
			} else {
				jQuery("#"+ObjID).append('<option value="" disabled="disabled" selected="selected">------</option>');
			}
		},"json"
	);
}

function fillLocals(CountyID,ObjID) {
	jQuery("#"+ObjID).find("option").remove();
	jQuery.post("/Resources/Php/getLocals.php", {CountyID:CountyID},
		function(data) {
			if(data.returns != '') {
				var local;
				var counties = data.returns.split(",");
				for(var i=0; i < counties.length; i++) {
					local = counties[i].split("-");
					jQuery("#"+ObjID).append('<option value="'+local[0]+'">'+local[1]+'</option>');
				}
			} else {
				jQuery("#"+ObjID).append('<option value="" disabled="disabled" selected="selected">------</option>');
			}
		},"json"
	);
}

function submitForm(FormID) {
	jQuery('#'+FormID).submit();	
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function exchange() {
	if(el('exchange_sum').value!="") {
		var from=parseFloat(el('exchange_sum').value.replace(",","."));
		var result=from*el('exchange_from').value/el('exchange_to').value;
		result=result.toFixed(2).replace(".",",");
		el('exchange_result').value=addCommas(result);
	} else
		el('exchange_result').value=0;
}

function open_popup(file,width,height) {
	if(width!=0 && height!=0)
		var wh = ",width="+width+",height="+height;
	else
		var wh = "";
	window.open(file,"mywindow","menubar=0,resizable=0,scrollbars=1"+wh);
}

function showInfo(InfoContent) {
	jQuery("#info-div").html(InfoContent).fadeIn("slow");											  
	jQuery().mousemove(function(e) {
		jQuery("#info-div").css("left",parseInt(e.pageX+10)+"px").css("top",parseInt(e.pageY+10)+"px");
	});
}

function hideInfo() {
	jQuery("#info-div").css("display","none");
}

jQuery(document).ready(function (){
	if(el('recomandam'))
		temp = setTimeout("slideRecommendedProducts(0,\"recomandam-slide\")",10000);
	if(el('product-suppliers'))
		tempSup = setTimeout("slideProductSuppliers(0,\"product-suppliers-slide\")",10000);
	
	/*jQuery("#page-wrapper").before('<div id="pending"></div>');
	jQuery("#pending").html("<div style=\"margin-top:250px;\">Service Pending</div>").css("display","block").css("background","#000").css("position","absolute").css("z-index","999").css("width","100%").css("height",jQuery(document).height() + 'px').css("font-size","20px").css("color","#FF6600").css("text-align","center").css("vertical-align","middle").css("opacity","0.6").fadeIn(300);*/
});