// $Id: common.js,v 1.22 2010/05/21 19:53:55 ntai Exp $
if (typeof(sik) == 'undefined') { var sik = {}; }

// redirect all external links to bridge page
sik.check_redirect = function(){
	if (document.location.href.match(/redirect\/\?opt/)) { return false; }
	var redirect = 'http://www.sikids.com/redirect/?option=';
	$("a[href^='http://']")
	.not("[href*='sikids.com']")
	.not("[href*='aol.com']")
	.not("[href*='ad.doubleclick']")
	.not("[href*='timeinc.net']")
	.not("[href*='nflrush.com']")
	.not("[href*='hyperionbooksforchildren.com']")
	.attr("href", function(){
	   var thishref = $(this).attr("href");
		$(this).attr("href",redirect+thishref);
	});	

	 //remove submit button to prevent multiple form submissions
	 $('#webform-client-form-4225').submit(function(){
		 $(this).append("Validating code...");
		 $('input[type=submit]').hide();
	  });

}; // sik.check_redirect()

// 'sort by' dropdown.
sik.sort_by = function(){
		$("#sifk_sortby .dropdown").hover(function() {
		$(this).find("dl").show();
	}, function() {
		$(this).find("dl").hide();		
	});
}; // sik.sort_by()

// wide layout right fail sorting dropdown
sik.right_sort = function(){
	$(".sort_dropdown li a.toggle").toggle(function() {
		$(this).parent("li").addClass("clicked");
		$(this).parent("li").find("ul").show("fast");
		// $(this).find("ul").show("fast");
		// 		$(this).find("a.toggle").addClass("clicked");
	}, function() {
		$(this).parent("li").find("ul").hide("fast");
		$(this).parent("li").removeClass("clicked");
	});
}; // sik.right_sort()

// subscribe box focus and blur for text boxes
sik.subscribe = function(){
	$("#sifk_subscribe input").focus(function() {
		if ($(this).attr("name") != "address2") {
			var inputValue = this.value;
			switch (inputValue) {
				case "Name": this.value = ""; break;
				case "Address": this.value = ""; break;
				case "City": this.value = ""; break;
				case "Zip/PO": this.value = ""; break;
			}
		}
	});
	$("#sifk_subscribe input").blur(function() {
		if ($(this).attr("name") != "address2") {
			if ($(this).attr("name") == "name" && this.value == "") {
				this.value = "Name";
			}
			if ($(this).attr("name") == "address1" && this.value == "") {
				this.value = "Address";
			}
			if ($(this).attr("name") == "city" && this.value == "") {
				this.value = "City";
			}
			if ($(this).attr("name") == "zip" && this.value == "") {
				this.value = "Zip/PO";
			}
		}
	});
}; // sik.subscribe()

// grab query parameters from the url
sik.get_query_parameters = function(){
	if(location.search != ''){
		var get_args = location.search.replace(/\?/,'').split('&');
		var params = new Object();
		
		for(var i = 0; i < get_args.length ;++i){
			var param = get_args[i].split('=');
			params[param[0]] = unescape(param[1]);
		}
		return params;
	}
	return '';
}; // sik.get_query_parameters()

// teams and Left rail input text to switch focus/blur states
sik.input_text = function() {
	$("#sifk_teamselector .search").focus(function() {
		if (this.value == "search") {
			this.value = "";
		}
	});
	$("#sifk_teamselector .search").blur(function() {
		if (this.value == "") {
			this.value = "search";
		}
	});
	$("#sifk_signin input").focus(function() {
		if (this.value == "SIKIDS ID" || this.value == "password") {
			this.value = "";
		}
	});
	$("#sifk_signin .sikids_id").blur(function() {
		if (this.value == "") {
			this.value = "SIKIDS ID";
		}
	});
	$("#sifk_signin .password").blur(function() {
		if (this.value == "") {
			this.value = "password";
		}
	});
}; // sik.input_text()

// wide layout's login button dropdown
sik.login_drop = function() {
	$("#sifk_wide_login").hover(function() {
		if (this.className != "sifk_loggedin") {
			$("#sifk_wide_loginbox").show();
			$(this).addClass("login_over");
		}
	}, function() {
		if (this.className != "sifk_loggedin") {
			$("#sifk_wide_loginbox").hide();
			$(this).removeClass("login_over");
		}
	});
	$("#sifk_wide_loginbox input").focus(function() {
		if (this.value == "SIKIDS ID" || this.value == "password") {
			this.value = "";
		}
	});
	$("#sifk_wide_loginbox .sikids_id").blur(function() {
		if (this.value == "") {
			this.value = "SIKIDS ID";
		}
	});
	$("#sifk_wide_loginbox .password").blur(function() {
		if (this.value == "") {
			this.value = "password";
		}
	});
} // sik.login_drop()

sik.cookie = function(){
	return{
		set : function(name, value, hours, path, domain, secure) {
			if (navigator.cookieEnabled) {
				var num_hours = 0;
				var not_NN2 = ( navigator && navigator.appName
							&& (navigator.appName == 'Netscape')
							&& navigator.appVersion
							&& (parseInt(navigator.appVersion) == 2) ) ? false : true;

				if (hours && not_NN2) {
					if ((typeof(hours) == 'string') && Date.parse(hours)) {
						num_hours = hours;
					} else if (typeof(hours) == 'number') {
						num_hours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
					}
				}

				document.cookie = name + '=' + escape(value) + ((num_hours)?(';expires=' + num_hours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':'');
			}
			return false;
		}, // sik.cookie.set()

		get : function(name) {
			if (document.cookie == '') {
				return false;
			} else {
				var first_char, lastChar;
				var the_big_cookie = document.cookie;
				first_char = the_big_cookie.indexOf(name);
				var nn2_hack = first_char + name.length;
				if ((first_char != -1) && (the_big_cookie.charAt(nn2_hack) == '=')) {
					first_char += name.length + 1;
					last_char = the_big_cookie.indexOf(';', first_char);
					if (last_char == -1) last_char = the_big_cookie.length;
					return unescape(the_big_cookie.substring(first_char, last_char));
				} else { return false;  }
			}
		}, // sik.cookie.get()

		kill : function(name, path, domain) {
			var value = get( name );
			if (value) {
				document.cookie = name + '=' + value + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');
			}
		} // sik.cookie.kill()
	}
}(); // sik.cookie

sik.disclaimer = function(){
	// set hours to 1 year
	var cookie_name = 'sik_disclaimer', cookie_value = true, cookie_hours = 8760;
	var DISCLAIMER_ON = true;
	
	function disclaimer_text_on(){
		if(!$('#sifk_disclaimer')){
			// check if there is no text in the disclaimer
			return false;
		}
		return true;
	}
	
	return {
		load : function(){
			if(DISCLAIMER_ON && !sik.cookie.get(cookie_name)){
				//alert('disclaimer on');
				// check if disclaimer is on + if cookie does not exist then register click callback for disclaimer anchor
				$('#sifk_disclaimer a').click(function(){
					$('#sifk_disclaimer').hide('normal');
					sik.cookie.set(cookie_name, cookie_value, cookie_hours);
					// interrupts page refresh
					return false;
				});
				$('#sifk_disclaimer').show('normal');
			}
		}
	};
}(); // sik.disclaimer()

// navigation namespace object that sets up navigation hover on / off states
sik.nav = function(){

	return {
		// top navigation on/off state. dependent on body id.
		on : function(){
			var current_page = $('body').attr('id').split('sifk_section_')[1];
			var nav_element = null;
			
			if(current_page == 'home'){
				$('#sifk_nav_home').addClass('first_on');
			} else if(current_page != 'more'){
				$('#sifk_nav_' + current_page).addClass('on');
			}
		}, // sik.nav.on()
		//top navigation roll over state.
		hover : function() {
			var current_page = $('body').attr('id').split('sifk_section_')[1];
			$('#sifk_nav ul > .first').hover(
				function(){
					$(this).addClass('first_on');
				},
				function(){
					if($(this).attr('id').split('sifk_nav_')[1] != current_page){
						$(this).removeClass('first_on');
					}
				}
			);
			$('#sifk_nav ul > .middle').hover(
				function(){
					$(this).addClass('on');
				},
				function(){
					if($(this).attr('id').split('sifk_nav_')[1] != current_page){
						$(this).removeClass('on');
					}
				}
			);
			$('#sifk_nav ul > .drop').hover(
				function(){
					$('.drop').addClass('drop_on');
					$('.drop > ul').show();
				},
				function(){
					$('.drop').removeClass('drop_on');
					$('.drop > ul').hide();
				}
			);
		}
	};
}(); // sik.nav

// determine what happens when body finishes loading (callbacks, etc)
$(document).ready(function(){
	sik.check_redirect();
	sik.nav.on();
	sik.nav.hover();
	sik.sort_by();
	sik.subscribe();
	sik.right_sort();
	sik.input_text();
	sik.login_drop();
	sik.disclaimer.load();
}); // ready()

