$(document).ready(function() {
	if($.browser.msie && $.browser.version == "6.0"){
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$("#dialogbox-transparency").css({ "width": maskWidth, "height": maskHeight });
		$("#dialogbox-transparency").fadeIn(100);	
		$("#dialogbox-transparency").fadeTo(100, 0.8);	
		var winH = $(window).height();
		var winW = $(window).width();
		$("#dialogbox").css("top",  winH / 2 - $("#dialogbox").height() / 2);
		$("#dialogbox").css("left", winW / 2 - $("#dialogbox").width() / 2);
		$("#dialogbox").fadeIn(100);
	}
	
	$("#cart #cart-content").mouseover(function(){
		$("#view_products_cart").css("display", "block");
		//$("#view_products_cart .scroll-pane").jScrollPane();
		$("#view_products_cart").css("height", "auto")
	});
	
	$("#cart #cart-content").mouseout(function(){ $("#view_products_cart").css("display", "none"); });
	
	$("#hdmenu li, #catalog-categories li").hover(
		function() { $(this).addClass("iehover"); },
		function() { $(this).removeClass("iehover"); }
	);
	
	rollover_image(".prerollover");
	
	$("a[rel='image']").colorbox({ transition: "fade" });
	
	if($("#detailed-item").length > 0) font_size_changer("#detailed-item", "news", 11, 13);
});

function rollover_image(hierarchy_elements){
	// Preload all rollovers
	$(hierarchy_elements + " img").each(function(){
		// Set the original src
		rollsrc = $(this).attr("src");
		rollON = rollsrc.replace(/.png$/ig,"-h.png");
		$("<img>").attr("src", rollON);
	});
	
	// Navigation rollovers
	$(hierarchy_elements + " a").mouseover(function(){
		imgsrc = $(this).children("img").attr("src");
		matches = imgsrc.match(/-h/);
		
		// don't do the rollover if state is already ON
		if (!matches){
			imgsrcON = imgsrc.replace(/.png$/ig,"-h.png"); // strip off extension
			$(this).children("img").attr("src", imgsrcON);
		}
		
	});
	$(hierarchy_elements + " a").mouseout(function(){
		$(this).children("img").attr("src", imgsrc);
	});
}


/*$*$*$*$* cookie management */

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);
}

/*$*$*$*$* font size changer */

function decreaseFont(container, unique_id, min_size){
	var currentFontSize = container.css("font-size");
	var currentFontSizeNum = parseInt(currentFontSize, 10);
	if(currentFontSizeNum > min_size)
	var newFontSize = currentFontSizeNum - 1;
	container.css("font-size", newFontSize);
	createCookie("text_size_" + unique_id, container.css("font-size"), 7);	
	return false;
}

function increaseFont(container, unique_id, max_size){
  	var currentFontSize = container.css("font-size");
 	var currentFontSizeNum = parseInt(currentFontSize, 10);
	if(currentFontSizeNum < max_size)
    var newFontSize = currentFontSizeNum + 1;
	container.css("font-size", newFontSize);
	createCookie("text_size_" + unique_id, container.css("font-size"), 7);	
	return false;
}

function font_size_changer(target_area, unique_id, min_size, max_size){
	if(readCookie("text_size_" + unique_id) == null) createCookie("text_size_" + unique_id, $(target_area).css("font-size"), 7);
	if(readCookie("o_text_size_" + unique_id) == null) createCookie("o_text_size_" + unique_id, $(target_area).css("font-size"), 7);
	$(target_area).css("font-size", readCookie("text_size_" + unique_id));
	
	// Increase Font Size
	$(".increaseFont").click(function(){ increaseFont($(target_area), unique_id, max_size); });
	// Decrease Font Size
	$(".decreaseFont").click(function(){ decreaseFont($(target_area), unique_id, min_size); });
}

/*$*$*$*$* modal content */

function showModalContent(content, css_style){
	$("#basic-modal-content").html(content);
	$("#basic-modal-content").modal();
	$("#simplemodal-container").css(css_style);
	$("#simplemodal-container").css({
		"position" : "absolute",
		"top": ($(window).height() - $("#simplemodal-container").height()) / 2 + $(window).scrollTop() + "px",
		"left": ($(window).width() - $("#simplemodal-container").width()) / 2 + $(window).scrollLeft() + "px"
	});
}

function reloadCaptcha(captcha){
	captcha.src = captcha.src + "?" + new Date();
}

/*$*$*$*$* custom radio/checkbox */

function customLabel(){
	if($(".label_check input").length){
		$(".label_check").each(function(){ $(this).removeClass("c_on"); });
		$(".label_check input:checked").each(function(){ $(this).parent("label").addClass("c_on"); });                
	};
	if($(".label_radio input").length){
		$(".label_radio").each(function(){ $(this).removeClass("r_on"); });
		$(".label_radio input:checked").each(function(){ $(this).parent("label").addClass("r_on"); });
	};
}


/*$*$*$*$* j utils */

function chars_left(limitField, limitCount, limitNum) {
	if(limitField.value.length > limitNum){
		limitField.value = limitField.value.substring(0, limitNum);
	}else{
		limitCount.value = limitNum - limitField.value.length;
	}
}

function submitenter(myfield, e){
	var keycode;
	
	if(window.event){
		keycode = window.event.keyCode;
	}else if(e){
		keycode = e.which;
	}else return true;
	
	if(keycode == 13){
		myfield.form.submit();
		return false;
	}else return true;
}

/*$*$*$*$* Google Map initialize */

function initialize(){
	var myOptions = {
		zoom: 16,
		center: new google.maps.LatLng(44.308776, 23.812348),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("google_map_content"), myOptions);
	var image = "img/common/logo-map-2use.png";
	var myLatLng = new google.maps.LatLng(44.308776, 23.812348);
	var logoMarker = new google.maps.Marker({
		position: myLatLng,
		map: map,
		icon: image
	});
}

/*$*$*$*$* submit form */

function testKeyword(form, field_name, keyword_form, alert_message) {
	var keyword = document.forms[form][field_name].value;
	var trimmed = keyword.replace(/^\s+|\s+$/g, "");
	
	if(trimmed.length == 0 || keyword == keyword_form){
		alert(alert_message);
		return false;
	}
	
	return true;
}

function submit_form(form, field_name, keyword, alert_message){
	if(testKeyword(form, field_name, keyword, alert_message) == true) document.forms[form].submit();
}

function submit_enter(object, event, form, field_name, keyword, alert_message){
	var keycode;
	
	if(window.event){
		keycode = window.event.keyCode;
	}else if(event){
		keycode = event.which;
	}else{
		return true;
	}

	if (keycode == 13){
		submit_form(form, field_name, keyword, alert_message);
		return false;
	}
	
	return true;
}

/*$*$*$*$* input limits */

function decrement_value(id_input, min_value){
	var input = document.getElementById(id_input);
	if(input.value > min_value){ input.value--; }
	if(input.value == ""){ input.value = min_value; }
}

function increment_value(id_input, max_value){
	var input = document.getElementById(id_input);
	if(input.value < max_value){ input.value++; }
	if(input.value == ""){ input.value = min_value; }
}

function toBeBetween(input, min_value, max_value){
	if(parseInt(input.value) > max_value){ input.value = max_value; }
	if(parseInt(input.value) < min_value){ input.value = min_value; }
}

function limitText(limitField, maxVal){
	var maxValLen = (maxVal + "").length;
	
	if(parseInt(limitField.value) < 0){
		limitField.value = 0;
	}
	
	if(limitField.value.length >= maxValLen){
		limitField.value = limitField.value.substring(0, maxValLen);
		
		if(limitField.value > maxVal){
			limitField.value = limitField.value.substring(0, maxValLen - 1);
		}
		if(limitField.value == maxVal){
			limitField.value = maxVal;
		}
	}
}

function numbersonly(e, decimal){
	var key;
	var keychar;

	if(window.event){
		key = window.event.keyCode;
	}else if(e){
		key = e.which;
	}else{
		return true;
	}
	
	keychar = String.fromCharCode(key);
	
	if((key == null) || (key == 0) || (key == 8) ||  (key == 9) || (key == 13) || (key == 27)){
		return true;
	}else if((("0123456789").indexOf(keychar) > -1)){
		return true;
	}else if(decimal && (keychar == ".")){
		return true;
	}else{
		return false;
	}
}

/*$*$*$*$* open pop-up windows */

var WindowObjectReference = null; // global variable

function openRequestedPopup(strUrl, strWindowName, width, height){
	var wleft = parseInt((screen.availWidth/2) - (width/2));
	var wtop = parseInt((screen.availHeight/2) - (height/2));
	
	// IE5 and other old browsers might allow a window that is partially offscreen or wider than the screen. Fix that.
	if (wleft < 0) {
		w = screen.width; wleft = 0;
	}
	if (wtop < 0) {
		h = screen.height; wtop = 0;
	}
	
	if(WindowObjectReference == null || WindowObjectReference.closed){
		WindowObjectReference = window.open(strUrl, strWindowName, "width=" + width + ",height=" + height + ",menubar=1,resizable=no,left=" + wleft + ",top=" + wtop + ",scrollbars=yes,status=yes");
	}else{
		WindowObjectReference.focus();
	};
}
