(function($) {
	// overflow meni
	$(function() {
		var winWidth = $(window).width();
		if (winWidth > 960) {
			$(".MenuBackground").css({
				width: winWidth / 2 + 323
			});
		}
	});

	// glavni izbornik 
	$(function() {
		$(".MenuContainer > li").hover(
			function() {
				$(this)
					.addClass("hover")
					.find(">div")
						.stop(true, true)
						.delay(100)
						.slideDown(50)
			}, 
			function() {
				$(this)
					.removeClass("hover")
					.find(">div")
						.stop(true, true)
						.delay(250)
						.slideUp(100)
			}
		);

		// ako nema treæe razine druga je okomita
		$(".MenuContainer > li > .SubMenuBar").each (function() {
			if ($(".SubMenuBar", this).length == 0) {
				$(this).addClass("no_submenu");
			} else {
				//alert($(this).parent().html());
				$(this).css("left", -1 * $(this).parent().position().left);
			}
		});
	});

	// Orange bar izbornik
	$(function() {
		$(".OrangeContainer").fadeIn();
	});

	// slideshow 
	$(function() {
		$(".Slideshow").slider({
			next: "&nbsp;",
			prev: "&nbsp;"
		});
	});

	// Product Add/remove controls
	$(function() {
		$(".CommandMore").live("click", function(e){
			addQuantity($(this), 1);

			//if ($(this).attr('href') == "#") {
				e.preventDefault();
			//}	
		});
		$(".CommandLess").live("click", function(e){
			addQuantity($(this), -1);
			
			//if ($(this).attr('href') == "#") {
				e.preventDefault();
			//}
		});
		$(".TextQuantity").live("change", function(e){
			calculateTotal($(this).attr("ref"), $(this).val());
			eval($(this).siblings(".CommandChange").attr('href'));
			e.preventDefault();
		});
		
		$(".TextQuantity").trigger("change");
		
		function addQuantity(elm, change) {
			var $link = elm;
			var $textbox = $link.siblings('[ref="' + $link.attr("rel") + '"]');

			var newQuant = parseInt($textbox.val()) + change;
			if (newQuant <= 0) {
				newQuant = 1;
			}
			$textbox.val(newQuant);
			$textbox.trigger("change");
			
			//calculateTotal($link.attr("rel"), newQuant);
		}
		
		function calculateTotal(idProduct, newQuant) {
			newQuant = parseFloat(newQuant);
			var $total = $("#" + idProduct + "_totalPrice");
			var $price = parseLocalNum($("#" + idProduct + "_price").text());
			if (!!$total) {
				$total.text(formatLocalNum($price * newQuant));
			}			
		}
		
		function parseLocalNum(num) {
			// sa servera
			var dsep= ","; //
			var tsep=(dsep==".")?",":".";
			// na klijentu
			var _dsep = (1/2).toString().charAt(1);
			var _tsep = (_dsep==".")?",":".";
			
			num = num.replace(tsep, "");
			return +(num.replace(dsep, _dsep));
		}
		
		function formatLocalNum(num) {
			// sa servera
			var dsep= ","; //
			var tsep=(dsep==".")?",":".";
			// na klijentu
			var _dsep = (1/2).toString().charAt(1);
			var _tsep = (_dsep==".")?",":".";
		
			return num.toFixed(2).replace(_dsep, dsep);
		}
	});
	
	$("a.ShipmentInfo").live("click", function() {
		$(this).parent().parent().find("input[rel],textarea[rel]").each(function (i, elm) {
			var id = $(elm).attr("rel");
			var value = $("[id$='" + id + "']").val();
			$(elm).val(value);
		});
		
		return false;
	});
	
	$(function() {
		$(".ProductDetails .img-box a").fancybox();
	});
	
	// IE specific
	if ($.browser.msie && $.browser.version == "7.0") {
	}
})(jQuery);
