/*
*	RBK Hockey
*	Contains utility / interface functions for RBK Hockey site
*	
*	Requires jQuery library (http://www.jquery.com),
*	SWFObject (http://blog.deconcept.com/swfobject/),
*	sIFR (http://www.mikeindustries.com/sifr/)
*	
*	Taylan Pince (taylan@trapeze.com) - April 28, 2007
*/

if (typeof trapeze == "undefined") var trapeze = new Object();

trapeze.RBK = {
	
	media_path : "/",
	
	mark_popups : function() {
		$("a[rel*='newwin']").click(trapeze.RBK.open_popup);
	},
	
	open_popup : function() {
		window.open($(this).attr("href"), "rbkwin", "status=1,toolbar=0,location=1,menubar=0,directories=0,resizable=1,scrollbars=1,height=700,width=600");
		return false;
	},
	
	mark_js : function() {
		$("html").addClass("has-js");
	},
	
	mark_lists : function() {
		$("li:last-child").addClass("last-child");
		$("li:first-child").addClass("first-child");
	},
	
	mark_forms : function() {
		$("input[@type=text]").addClass("text");
		$("input[@type=password]").addClass("text");
		$("input[@type=file]").addClass("file");
		$("input[@type=radio]").addClass("radio");
		$("input[@type=checkbox]").addClass("checkbox");
		$("input[@type=image]").addClass("image");
	},
	
	mark_separators : function() {
		$("hr").wrap('<div class="hr"></div>');
	},
	
	mark_iepng : function() {
		if ($.browser.msie && parseInt($.browser.version) < 7) {
			$("#Navigation, #NavigationStopper, #NavigationWrapper > ul, body:not(.your-move) #RightColumn, #LeftColumn h2").each(function() {
				var bg = this.currentStyle.backgroundImage;
				var method = (this.currentStyle.backgroundRepeat == "no-repeat") ? "crop" : "scale";
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='" + method + "', src='" + bg.substring(5, bg.length - 2) + "')";
				this.style.backgroundImage = "url('" + trapeze.RBK.media_path + "images/blank.png')";
				$(this).find("a").css("position", "relative");
			});
		}
	},
	
	init_sifr : function() {
		if (typeof sIFR == "function") {
			sIFR.replaceElement(named({sSelector:"body.your-move #RightColumn h2", sFlashSrc:this.media_path + "flash/pirulen.swf", sColor: "#69F124", sLinkColor: "#69F124", sWmode: "transparent", nPaddingLeft: 18}))
			sIFR.replaceElement(named({sSelector:"body.your-move h1, body.your-move h2, body.your-move h3.sifr", sFlashSrc:this.media_path + "flash/pirulen.swf", sColor: "#69F124", sLinkColor: "#69F124", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"h1.en, h1.fr, h1.de, h1.fi", sFlashSrc:this.media_path + "flash/rbk-bold.swf", sColor:"#FFFFFF", sLinkColor:"#FFFFFF", sBgColor:"#FFFFFF", sHoverColor:"#FFFFFF", sWmode:"transparent"}));
			sIFR.replaceElement(named({sSelector:"#InsiderCall h2.bottom", sFlashSrc:this.media_path + "flash/rbk-bold.swf", sColor:"#445a6f", sLinkColor:"#445a6f", sBgColor:"#FFFFFF", sHoverColor:"#999999", nPaddingLeft:10, nPaddingRight:10, sFlashVars:"textalign=center"}));
			sIFR.replaceElement(named({sSelector:"#InsiderCall h2.top", sFlashSrc:this.media_path + "flash/rbk-bold.swf", sColor:"#445a6f", sLinkColor:"#445a6f", sBgColor:"#FFFFFF", sHoverColor:"#445a6f", nPaddingLeft:10, nPaddingRight:10}));
		};
	},
	
	preload_list : ["images/bg-nav-active.png", "images/bg-nav-active-right.png", "images/bg-subnav.png"],
	
	init_preload : function() {
		for (var i = 0; i < trapeze.RBK.preload_list.length; i++) {
			var img = new Image(1, 1);
			img.src = trapeze.RBK.media_path + trapeze.RBK.preload_list[i];
		}
	},
	
	active_nav : null,
	
	init_nav : function() {
		$("#Navigation > div > ul > li > a").wrap('<div class="link-wrapper"></div>');
		
		setTimeout("trapeze.RBK.init_nav_width()", 100);
		
		trapeze.RBK.active_nav = $("#Navigation").find("li.active");
		
		$("#Navigation").find("li").hover(function() {
			if ($(this).parents("ul").size() == 1) {
				$("#Navigation").find("li.active").removeClass("active");
			}
			
			$(this).addClass("active").children("ul").css("display", "block");
		}, function() {
			$(this).removeClass("active").children("ul").css("display", "none");
			
			if (trapeze.RBK.active_nav != null && $(this).parents("ul").size() == 1) {
				trapeze.RBK.active_nav.addClass("active");
			}
		}).each(function() {
			if ($(this).find("ul").size() > 0) {
				$(this).addClass("expand")
			}
		});
	},
	
	init_nav_width : function() {
		var nav_width = 0;
		
		$("#Navigation > div > ul > li").each(function() {
			nav_width += $(this).width() + parseInt($(this).css("margin-right")) + parseInt($(this).css("padding-left"));
		});
		
		if ($.browser.msie && parseInt($.browser.version) < 7) {
			$("#NavigationWrapper").append('<div id="NavigationStopper">&nbsp;</div>');
			
			nav_width += $("#NavigationStopper").width();
		}
		
		nav_width += parseInt($("#Navigation > div").css("padding-right"));
		
		$("#Navigation").width(nav_width);
		
		this.mark_iepng();
	},
	
	language_switch_width : 0,
	
	init_language_switch : function() {
		$(".language-switch-form").find("input[@type=submit]").each(function() {

            $(this).replaceWith('<div onclick="$(this).parents(\'form.language-switch-form\').submit();" class="language-switch-btn" style="cursor:pointer;">'+ $(this).val() + '</div>');

		/*
			$(this).replaceWith('<a href="javascript:void(0);" onclick="$(this).parents(\'form.language-switch-form\').submit();">' + $(this).val() + '</a>');
        */
		});
		
		$("#LanguageSwitchMenu > li").each(function() {
			trapeze.RBK.language_switch_width += $(this).width() + parseInt($(this).css("padding-left")) + parseInt($(this).css("margin-right"));
		});
		
		trapeze.RBK.language_switch_width += 2;
		
		$("#LanguageSwitchMenu").width($("#LanguageSwitchMenu > li:first-child").width() + parseInt($("#LanguageSwitchMenu > li:first-child").css("padding-left")) + parseInt($("#LanguageSwitchMenu > li:first-child").css("margin-right")));
		
		$("#LanguageSwitchLink").click(function() {
			if ($("#LanguageSwitchMenu").width() < trapeze.RBK.language_switch_width) {
				$("#LanguageSwitchMenu").animate({
					width: trapeze.RBK.language_switch_width
				}, 1000);
			} else {
				$("#LanguageSwitchMenu").animate({
					width: $("#LanguageSwitchMenu > li:first-child").width() + parseInt($("#LanguageSwitchMenu > li:first-child").css("padding-left")) + parseInt($("#LanguageSwitchMenu > li:first-child").css("margin-right"))
				}, 1000);
			}
		}).attr("href", "javascript:void(0);").css("display", "inline");
	},
	
	init_downloads : function() {
		$("#Downloads > li").hover(function() {
			$(this).css("position", "relative").find("ul").css("display", "block");
		}, function() {
			$(this).css("position", "static").find("ul").css("display", "none");
		});
	},
	
	init : function() {
		this.mark_js();
		this.mark_lists();
		this.init_sifr();
		this.init_preload();
		this.mark_forms();
		this.mark_separators();
		this.mark_popups();
		this.init_nav();
		this.init_language_switch();
		this.init_downloads();
	}
	
};

$(function() {
	trapeze.RBK.init();
});
