var sidebar_redraw = function () {
	var $sidebar = $("#sidebar");
	var $sidebar_inner = $("#sidebar_inner");
	
	if ($("#sidebar.product_sidebar").width() > 400) {
		$("#sidebar.product_sidebar").width(400);
	}
	
	$("#sidebar .sidebar_l").height($sidebar.outerHeight());
	$("#sidebar .sidebar_r").height($sidebar.outerHeight());
	$("#sidebar .sidebar_t").width($sidebar_inner.outerWidth());
	$("#sidebar .sidebar_b").width($sidebar_inner.outerWidth());
}

var style_switch = function () {
	var new_value = $.cookie("enable_large_fonts") == 1 ? 0 : 1;
	$.cookie("enable_large_fonts", new_value, {path:'/', expires: 365});
	window.location = window.location;
}

var play_video = function (video_path) {
	$("#mares")[0].loadVideo(video_path);
	$("html,body").scrollTop(0);
	return false;
}

$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
while(x<c.length){var m=r.exec(c.substr(x));
  if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
  }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
  o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});

$(function() {
	// SIDEBAR
	if ($("#sidebar.product_sidebar").width() > 400) {
		$("#sidebar.product_sidebar").width(400);
	}
	
	var $sidebar_inner = $("#sidebar_inner");
	if ($sidebar_inner.length) {
		var $main_inner = $("#main_inner:not(.do_not_resize_main)");
		if ($main_inner.length > 0) {
			$main_inner.width($main_inner.width() - $sidebar_inner.outerWidth() - parseInt($sidebar_inner.css('marginLeft')) - parseInt($sidebar_inner.css('marginRight')) - parseInt($sidebar_inner.css('paddingLeft')) - parseInt($sidebar_inner.css('paddingRight')));
		} else {
			$h2 = $("div#product>h2:first, #main_inner>h2");
			$h2.width($h2.width() - $sidebar_inner.outerWidth() - parseInt($sidebar_inner.css('marginLeft')) - parseInt($sidebar_inner.css('marginRight')) - parseInt($sidebar_inner.css('paddingLeft')) - parseInt($sidebar_inner.css('paddingRight')));
		}

		var $sidebar = $("#sidebar");
		$sidebar.prepend(
			"<div class='sidebar_t' style='width:" + $sidebar_inner.outerWidth() +  "px'></div>" +
			"<div class='sidebar_b' style='width:" + $sidebar_inner.outerWidth() +  "px'></div>" +
			"<div class='sidebar_l' style='height:" + $sidebar.outerHeight() +  "px'><div></div></div>" +
			"<div class='sidebar_r' style='height:" + $sidebar.outerHeight() +  "px'><div></div></div>" +
			"<div class='sidebar_bl'></div>" +
			"<div class='sidebar_br'></div>"
		);
		
		var sidebar_total_height = $("#sidebar").outerHeight() + $("#sidebar_inner2").outerHeight();
		if ($("#main").height() < sidebar_total_height) $("#main").height(sidebar_total_height);
	}
	
	// FLASH MENU
	var params = {
		menu:"false",
		allowScriptAccess:"sameDomain",
		wmode:"opaque",
		allowFullScreen:"false"
	};
	var attributes = {
		id:"mares",
		name:"mares"
	};
	swfobject.embedSWF(leovince_root_path + "/header.swf","flash_div", "989", "379", "9.0.0", leovince_root_path + "/fill_installer.swf", flashvars, params, attributes);

	// INJECTING SPAN INTO H2 IF NOT PRESENT
	$("h2").each(function () {
		var $this = $(this);
		if ($this.find(">span").length == 0) {
			$this.wrapInner("<span></span>");
		}
	});
	
	// FLIR
	var img_margin = ($("h2:first img.material").length > 0) ? parseInt($("h2:first img.material").css('marginRight')) : 0;
	$("h2:first span").css("display", "block").width($("h2:first").innerWidth() - $("h2:first img.material").width() - img_margin - 6);
	var flir_style = new FLIRStyle({mode:'wrap'});
	FLIR.init({path: leovince_root_path + "/facelift/"});
	$("h2 span, .mc_title_stage").each(function() {
		FLIR.replace(this, flir_style);
	});
	$("h2:first span").css("display", "inline");
	
	// FOOTER
	$("#footer img.flag").each(function () {
		var $this = $(this);
		var tmp = $this.attr("src").split("-");
		tmp = tmp[tmp.length-1].split(".")[0];
		if (tmp == "off") {
			$this.hover(function () {
				var $this = $(this);
				$this.attr("src", $this.attr("src").replace("off", "on"));
			}, function () {
				var $this = $(this);
				$this.attr("src", $this.attr("src").replace("on", "off"));
			});
		}
	});
});
