/*
*	Requires jQuery library (http://www.jquery.com),
*	SWFObject (http://blog.deconcept.com/swfobject/),
*	cufon (http://wiki.github.com/sorccu/cufon)
*/

/*Cufon.replace('.page-title');*/
Cufon.replace('.section-title');
Cufon.replace('.date-title');
Cufon.replace('#WeekShowcaseBlurb h2');

jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i = a.length -1; i > 0; i--) {
		jQuery("<img />").attr("src", a[i]);
	}
}

if (typeof trapeze == 'undefined') trapeze = new Object();

trapeze.RachaelRay = {

	set_cookie : function(name, val, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		} else {
			var expires = "";
		}
		document.cookie = name + "=" + val + expires + "; path=/";
	},

	get_cookie : function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},

	del_cookie : function(name) {
		this.set_cookie(name, '', -1);
	},

	check_cookie : function() {
		if ($("#UserBoxSignInForm").size() > 0) {
			if (this.get_cookie('RR2CookieTester') == null) {
				this.set_cookie('RR2CookieTester', 'True', 0.25);
				if (this.get_cookie('RR2CookieTester') == null) {
					$("#UserBoxSignInForm").html("<p>Cookies are required for logging in to Club RR. Please enable them through your browser settings.</p>");
				}
			}
		}
	},

	section : function() {
		if ($("body").attr("class") && $("body").attr("class").indexOf("home") > -1) {
			return "home";
		} else {
			return "sub";
		}
	},

	setup_video : function(videoId, videoPath, htmlId) {
		/*var so = new SWFObject(this.assets_path + "fla/player_small.swf", videoId, "331", "280", "8", "#FFFFFF");
		so.addParam("allowScriptAccess", "always");
		so.addParam("scale", "noscale");
		so.addParam("salign", "T");
		so.addParam("wmode", "transparent");
		so.addVariable("front", "false");
		so.addVariable("xmlPath", videoPath);
		so.addVariable("prefPath", this.assets_path + "xml/player-prefs.xml");
		so.addVariable("allowedDomain", window.location.hostname);
		so.write(htmlId);*/

		/*
		commented out, this needs to be handled in the swf not appended html
		~ patrick.may 2009.05.17
		var videoBox = $('#' + htmlId);
		var host = "http://" + location.hostname;
		var flashVars = '"xmlPath=' + host + '/show/view/667/player/&amp;prefPath=' +
				this.assets_path + 'xml/player-prefs.xml"';
		var playerPath = this.assets_path + 'fla/player_small.swf';
		var embedText = '<object width="331" height="280">' +
				'<param name="movie" value="' + playerPath + '"></param>' +
				'<param name="flashvars" value=' + flashVars + '></param>' +
				'<embed src="' + playerPath + '" type="application/x-shockwave-flash"' +
				'width="331" height="280" flashvars=' + flashVars + '></embed></object>';
		var embedId = htmlId + "embed";
		var embed = $("<input type=\"text\" />").attr("id", embedId);
		embed.attr("value", embedText).attr("readonly", "readonly");
		embed.click(function() {
				$("#" + embedId).focus().select();
			});
		$("<label>Embed </label>").attr("for", embedId).appendTo(videoBox);
		embed.appendTo(videoBox);*/
	},

	init_showcase : function(path) {
		if ($("#WeekShowcaseVideo").size() > 0 && (document.player_path || path)) {
			this.setup_video("WeekShowcaseVideoPlayer", (path) ? path : document.player_path, "WeekShowcaseVideo");
		}
	},

	init_giveaway : function() {
		if ("object" == typeof videos) {
			for (var i = 0; i < videos.length; i++) {
				var video = videos[i];
				this.setup_video("SegmentVideoPlayer" + video.id, video.path, "SegmentVideo" + video.id);
			}
		} else if ($("#ComingUpVideo").size() > 0) {
			this.setup_video("ComingUpVideoPlayer", path, "ComingUpVideo");
		}
	},

	mark_last : function() {
		$("li:last-child").addClass("last-child");
	},

	mark_first : function() {
		$("li:first-child").addClass("first-child");
		$("li:nth-child(2)").addClass("second-child");
		$("li:nth-child(3)").addClass("third-child");
	},

	clean_hr : function() {
		$("hr").wrap('<div class="hr"></div>');
	},

	welcome : function() {
		if (navigator.platform.indexOf("Linux") == -1) {
		  if ($("#WelcomeBox").size() > 0) {
				var section = this.section();
				var sub = (section == "home") ? "" : "-sub";
				var so = new SWFObject(this.assets_path + "fla/welcome.swf", "WelcomeFlash", 315, 175, "8", "#FFFFFF");
				so.addParam("wmode", "transparent");
				so.addVariable("flower", this.assets_path + "img/wel-sunflower" + sub + ".png");
				so.addVariable("image", this.assets_path + "img/wel-rachael" + sub + ".jpg");
				so.addVariable("flv", this.assets_path + "flv/2108clubrrpromo.flv");
				so.addVariable("section", section);
				so.write("WelcomeBox");
			}
		}
	},

	paginator : function() {
		$(".paginator form input").keyup(function () {
			this.value = this.value.replace(/[^0-9]/g,'');
    });
		$(".paginator form").submit(function() {
			var page = parseInt($(this).find("input").val());
			var pages = parseInt($(this).find("span.pages").text());
			if (page < 1) {
				page = 1;
			} else if (page > pages) {
				page = pages;
			}
			$(this).find("input").val(page);

			if ($(this).hasClass("by_path")) {
				var url = window.location.href;
				var page_re=/page\/\d+\//;
				var target_page_url = "page/"+page+"/";
				if (page_re.test(url)) {
					url = url.replace(page_re, target_page_url);
				}
				else {
					url = target_page_url;
				}
				window.location = url;
				return false;
			}
			return true;
		});
    },

	promo_be_on_the_show : function() {
		if ($("#BeOnTheShowTV").size() > 0) {
			var so = new SWFObject(this.assets_path + "fla/beontheshow.swf", "PromoBeOnTheShowFlash", "224", "147", "8", "#FFFFFF");
			so.addParam("allowScriptAccess", "always");
			so.addParam("scale", "noscale");
			so.addParam("salign", "T");
			so.addParam("wmode", "transparent");
			so.addVariable("front", "false");
			so.write("BeOnTheShowTV");
		}

	},

	promo_cook_with_rach : function() {
		if ($("#PromoCookWithRach").size() > 0) {
			var so = new SWFObject(this.assets_path + "fla/cookwithrach.swf", "PromoCookWithRachFlash", "613", "220", "8", "#FFFFFF");
			so.addParam("allowScriptAccess", "always");
			so.addParam("scale", "noscale");
			so.addParam("salign", "T");
			so.addParam("wmode", "transparent");
			so.addVariable("front", "false");
			so.write("PromoCookWithRach");
		}
	},

	// orphan_navs : new Array("tips-and-stories", "buddies", "celeb-friends", "behind-the-scenes"),

	// check_orphan_navs : function() {
	// 	for (var item in this.orphan_navs) {
	// 		var slug = this.orphan_navs[item];
    //         if ($("body." + slug).size() > 0) {
	// 			$("#SubNavigation").find("a." + slug).addClass("active");
	// 		}
	// 	}
	// },


	setNavigation : function() {
		if ($("#Navigation").size() > 0){
		$('#cssdropdown li.nav_head').hover(
			function() { $('ul', this).css('display', 'block'); },
			function() { $('ul', this).css('display', 'none'); });
	}},

	setStyleHome : function() {
		if ($("#MessageBoardBox").size() > 0) {
			$("#MessageBoardBox ul li").css('cursor', 'pointer').click(function() {
				window.location.href = $(this).find('h6 a').attr('href');
			});
		}
	},

	wireSearch : function(){
		$('#top_nav_search').focus(function(){
			if ($(this).val() == 'SEARCH BY KEYWORD')
				$(this).val('');

		});
		$('#top_nav_search').blur(function(){
			if ($(this).val() == '')
				$(this).val('SEARCH BY KEYWORD');

		});
	},

	init : function() {
		this.welcome();
		this.mark_last();
		this.mark_first();
		this.clean_hr();
		this.init_showcase();
		this.init_giveaway();
		this.paginator();
		this.promo_cook_with_rach();
		this.promo_be_on_the_show();
		this.check_cookie();
		// this.check_orphan_navs();

		this.setNavigation();

		this.setStyleHome();
		this.wireSearch();
	}

};

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

//////////////////// scroll ////////////////////////////////////////////////////////////////////////////
jQuery.getPos = function (e)
{
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e)
{
	if (e) {
		w = e.clientWidth;
		h = e.clientHeight;
	} else {
		w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	return {w:w,h:h};
};
jQuery.getScroll = function (e)
{
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};

jQuery.intval = function (v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};

jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};

jQuery.fx.ScrollTo = function (e, o)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t)};
	z.timer=setInterval(function(){z.step();},13);
};

