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

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");
}

function hideMessages() {
	jQuery("#messages-overlay").animate({opacity:0},500,function() { jQuery(this).remove(); });
	jQuery("#messages").animate({opacity:0},500,function() { jQuery(this).remove(); });
}

function showSubcategories(ID) {
	jQuery(".categories-menu").find(".active").each(function(){
		jQuery(this).addClass("inactive").removeClass("active");												
	});
	
	jQuery('#category-' + ID).removeClass("inactive").addClass("active");
	jQuery("#subcategories-" + ID).removeClass("inactive").addClass("active");
}

function initialize() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("google-map"));
		map.setCenter(new GLatLng(44.44762,26.223774), 13);
		
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		map.enableScrollWheelZoom()
		
		var artenter = new GLatLng(44.44762,26.223774);
		var artentermarker =new GMarker(artenter); 
		map.addOverlay(artentermarker);
		
		map.openInfoWindowHtml(artenter,"<div style=\"height:60px; width:150px; display:block; background:url(/Resources/Images/logo-white-google-map.png) no-repeat center; padding-top:20px;\"><b>ArtEnteR</b><br />Pantelimon, Str. SF. Calinic, Nr.11</div>");
	}
}

function slider(){
	var next = jQuery("#top-slider img.active-slider-image").next();
	if(next.attr("alt") != "image")
		next = jQuery("#top-slider img:first");
	
	next.css("display","block").css("opacity",1);
	
	jQuery("#top-slider .active-slider-image").animate({opacity:0},1000,function(){
		jQuery(this).removeClass("active-slider-image").addClass("hidden-slider-image");
		next.removeClass("hidden-slider-image").addClass("active-slider-image");
	});
	
	var timeout = setTimeout("slider()",6000);
}

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);
	if(el('messages')) {
		jQuery("#messages-overlay").css({"opacity":"0.5",
						  "height":jQuery(document).height() + "px"}); 
		jQuery("#messages").css({"left":(jQuery(window).width() - 400) / 2 + "px",
								  "top":(jQuery(window).height() - 150) / 2 + "px"});
		jQuery(window).scroll(function(){
			jQuery("#messages").css("top",(jQuery(window).height() - 150) / 2 + jQuery(window).scrollTop() + "px");							
		});
		
		jQuery(window).resize(function() {
			jQuery("#messages").css({"left":(jQuery(window).width() - 400) / 2 + "px",
									  "top":(jQuery(window).height() - 150) / 2 + "px"});
		});
		var temp = setTimeout("hideMessages()",4000);
	}
	
	if(jQuery('.page-content').height() < jQuery('#right-content').height())
		jQuery('.page-content:first').css('min-height',jQuery("#right-content").height() + 'px');
		
	/*jQuery(".categories-menu ul:first").mousemove(function(e){
		if(e.pageX > 920 && jQuery(this).css("left") == "0px") {
			jQuery(this).find("ul").addClass("inactive").css("left","700px");
			jQuery(this).animate({left:-700 + "px"},400);
		}
		
		if(e.pageX < 330 && jQuery(this).css("left") == "-700px") {
			jQuery(this).find("ul").addClass("inactive").css("left","0px");
			jQuery(this).animate({left:0 + "px"},400);
		}
	});*/ 
	
	jQuery("#top-slider ul").css("opacity",0.7);
	var timeout = setTimeout("slider()",6000);
	
	//jQuery(document).jSnow({flakes:50,zIndex:100,followScroll:true});
	
	//var width = jQuery(window).width();
	
	//jQuery('#sarbatori').css('right',((width-960)/2) + "px");	
	/*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);*/
});
