$(document).ready(function(){
	loadsidebar();
	if($("#header_scroller").length > 0) { scrollbanner_setup(); }
	$("#storenav").load("/mm5/merchant.mvc?Screen=LINK");
	$(".submenu").parent().hover(function(){
		$(this).children(".submenu").fadeIn();
	},function(){
		$(this).children(".submenu").hide();
	}).children("a").addClass("parent");
	if ($(".product_entry").length > 0) {
		clearprodentry($(".product_entry:eq(0)"));
	}
	
	if ($(".autocatch").length > 0) {
		setpopout();
	}
});

function scrollbanner_setup() {
	$("#scroller_images").append("<table id=\"scrolling_table\"><tbody><tr id=\"scroller_use_row\"></tr></tbody></table>")
	var scrollcount = $("#scroller_images").children("img").length;
	for (i=0;i<scrollcount;i++){
		$("#scroller_use_row").prepend("<td id=\"scroller" + i + "\"></td>");
		$("#scroller_images img:eq(0)").appendTo("#scroller" + i);
	}
	scrollbanner_gotoend();
	scrollingbanner = setInterval("scrollbanner(this)",50);
}

function scrollbanner(elem) {
	var curscroll = ($("#scroller_images").scrollLeft() == 0) ? $("#scroller_images").attr("rel") : $("#scroller_images").scrollLeft();
	$("#scroller_images").scrollLeft(curscroll-2);
}

function scrollbanner_gotoend() {
	var tablewidth = $("#scrolling_table").width();
	$("#scroller_images").attr("rel",tablewidth+700).scrollLeft(tablewidth+700);
}

function loadsidebar() {
	var forceunique = (new Date().getTime());
	$("#sidebar_column").load("/mm5/merchant.mvc?Screen=SOLC&Store_Code=xrail&uq=" + forceunique,function(){ loadproducts(forceunique); });
}

function loadproducts(forceunique) {
	$("#sidelinktree li").each(function(){
		var thishref = $(this).find("a").attr("href");
		var catcode = thishref.indexOf("Category_Code");
		var extracted = thishref.substr(14+catcode);
		var thisindex = $(this).parent().children().index(this);
		$(this).find(".prodlist_addition").hide().load("/mm5/merchant.mvc?Screen=SOLO&Store_Code=xrail&Category_Code=" + extracted + "&uq=" + forceunique + " .xml_prod_list",function(){ init_flyouts($("#sidelinktree").children("li:eq(" + thisindex + ")")); });
	});
}

function init_flyouts(elem) {
	$(elem).find("dd a").hover(function(){
		$(".prod_info_flyout").hide();
		$(this).parent().addClass("open").children(".prod_info_flyout").fadeIn(300);
	},function(){
		$(this).parent().removeClass("open").children(".prod_info_flyout").hide();
	});
	
	if ($(elem).find("dd").length > 0) {
		$(elem).children("a").append("<div class=\"slidetoggler\"></div>");
		$(elem).find(".slidetoggler").click(function(){
			if ($(".disablecomplexeffects").length == 0) {
				$(this).parent().parent().toggleClass("prodlist_open").children(".prodlist_addition").slideToggle(300);
			} else {
				$(this).parent().parent().toggleClass("prodlist_open").children(".prodlist_addition").toggle();
			}
			if ($(".prodlist_open").length > 0) {
				if ($("#videolinks").is(":visible")) {
					$("#videolinks").fadeOut(100);
				}
			} else {
				$("#videolinks").fadeIn(1000);
			}
			$(this).parent().blur();
			return false;
		});
	}
}

function clearprodentry(elem) {
	$(elem).addClass("firstinrow");
	if ($(elem).next().next()) {
		clearprodentry($(elem).next().next());
	}
}

function setpopout() {
	$(".autocatch a").click(function(){
		popit(this);
		return false;
	});
	testauto();
}

function testauto() {
	var hash = document.location.hash.substring(2);
	popit($("#" + hash));
}

function popit(elem) {
	$("#popoutbox").remove();
	var getsrc = $(elem).attr("href");
	$("body").append('<div id="popoutbox"><iframe id="popoutframe" src="' + getsrc + '" scrolling="no" frameborder="0" width="575px" height="355px"></iframe><p class="closeit">Close Video</p></div>');
	$(".closeit").click(function(){
		$("#popoutbox").remove();
	});
}