﻿var NStage = {}; // Namespace

NStage.Utils = {
	Fonts: function(){
		Cufon.replace('h1,ul.localNav a');
		Cufon.replace('h2', {
			color: '-linear-gradient(white, black)'
		});
	},
	JsCheck: function(){
		$("body").addClass("jsEnabled");
	},
	ClearDefaultVal: function(){
		
		/*
			Watermark v3.0.4 (January 14, 2010) plugin for jQuery
			http://jquery-watermark.googlecode.com/
			Copyright (c) 2009-2010 Todd Northrop
			http://www.speednet.biz/
			Licensed under GPL 3, see  <http://www.gnu.org/licenses/>
		*/
		(function(a){var k="function",i="password",c="maxLength",e="type",b=true,d=false,s="watermark",t,m=s,h="watermarkClass",p="watermarkFocus",j="watermarkSubmit",l="watermarkMaxLength",g="watermarkPassword",f="watermarkText",r=":data("+m+")",n=":text,:password,:search,textarea",o=["Page_ClientValidate"],q=d;a.extend(a.expr[":"],{search:function(a){return "search"===a.type},data:function(f,h,g){var e,c=/^((?:[^=!^$*]|[!^$*](?!=))+)(?:([!^$*]?=)(.*))?$/.exec(g[3]);if(c){e=a(f).data(c[1]);if(e!==t){if(c[2]){e=""+e;switch(c[2]){case "=":return e==c[3];case "!=":return e!=c[3];case "^=":return e.slice(0,c[3].length)==c[3];case "$=":return e.slice(-c[3].length)==c[3];case "*=":return e.indexOf(c[3])!==-1}}return b}}return d}});a.watermark={version:"3.0.4",options:{className:s,useNative:b},hide:function(b){a(b).filter(r).each(function(){a.watermark._hide(a(this))})},_hide:function(a,i){if(a.val()==a.data(f)){a.val("");if(a.data(g))if(a.attr(e)==="text"){var d=a.data(g),b=a.parent();b[0].removeChild(a[0]);b[0].appendChild(d[0]);a=d}if(a.data(l)){a.attr(c,a.data(l));a.removeData(l)}if(i){a.attr("autocomplete","off");window.setTimeout(function(){a.select()},0)}}a.removeClass(a.data(h))},show:function(b){a(b).filter(r).each(function(){a.watermark._show(a(this))})},_show:function(d){var r=d.val(),j=d.data(f),m=d.attr(e);if((r.length==0||r==j)&&!d.data(p)){q=b;if(d.data(g))if(m===i){var o=d.data(g),n=d.parent();n[0].removeChild(d[0]);n[0].appendChild(o[0]);d=o;d.attr(c,j.length)}if(m==="text"||m==="search"){var k=d.attr(c);if(k>0&&j.length>k){d.data(l,k);d.attr(c,j.length)}}d.addClass(d.data(h));d.val(j)}else a.watermark._hide(d)},hideAll:function(){if(q){a.watermark.hide(n);q=d}},showAll:function(){a.watermark.show(n)}};a.fn.watermark=function(q,l){var o="string",s=typeof q===o,r;if(typeof l==="object"){r=typeof l.className===o;l=a.extend({},a.watermark.options,l)}else if(typeof l===o){r=b;l=a.extend({},a.watermark.options,{className:l})}else l=a.watermark.options;if(typeof l.useNative!==k)l.useNative=l.useNative?function(){return b}:function(){return d};return this.each(function(){var u="dragleave",t="dragenter",w=this,d=a(w);if(!d.is(n))return;if(d.data(m)){if(s||r){a.watermark._hide(d);s&&d.data(f,q);r&&d.data(h,l.className)}}else{if(l.useNative.call(w,d))if((""+d.css("-webkit-appearance")).replace("undefined","")!==""&&d.attr("tagName")!=="TEXTAREA"){s&&d.attr("placeholder",q);return}d.data(f,s?q:"");d.data(h,l.className);d.data(m,1);if(d.attr(e)===i){var x=d.wrap("<span>").parent(),k=a(x.html().replace(/type=["']?password["']?/i,'type="text"'));k.data(f,d.data(f));k.data(h,d.data(h));k.data(m,1);k.attr(c,q.length);k.focus(function(){a.watermark._hide(k,b)}).bind(t,function(){a.watermark._hide(k)}).bind("dragend",function(){window.setTimeout(function(){k.blur()},1)});d.blur(function(){a.watermark._show(d)}).bind(u,function(){a.watermark._show(d)});k.data(g,d);d.data(g,k)}else d.focus(function(){d.data(p,1);a.watermark._hide(d,b)}).blur(function(){d.data(p,0);a.watermark._show(d)}).bind(t,function(){a.watermark._hide(d)}).bind(u,function(){a.watermark._show(d)}).bind("dragend",function(){window.setTimeout(function(){a.watermark._show(d)},1)}).bind("drop",function(b){var a=b.originalEvent.dataTransfer.getData("Text");d.val().replace(a,"")===d.data(f)&&d.val(a);d.focus()});if(w.form){var o=w.form,v=a(o);if(!v.data(j)){v.submit(a.watermark.hideAll);if(o.submit){v.data(j,o.submit);o.submit=function(c,b){return function(){var d=b.data(j);a.watermark.hideAll();if(d.apply)d.apply(c,Array.prototype.slice.call(arguments));else d()}}(o,v)}else{v.data(j,1);o.submit=function(b){return function(){a.watermark.hideAll();delete b.submit;b.submit()}}(o)}}}}a.watermark._show(d)}).end()};o.length&&a(function(){for(var b,c,d=o.length-1;d>=0;d--){b=o[d];c=window[b];if(typeof c===k)window[b]=function(b){return function(){a.watermark.hideAll();b.apply(null,Array.prototype.slice.call(arguments))}}(c)}})})(jQuery);		
		
		//set default text for text fields from value or title
		$("input[type='text'],textarea").each(function(){
			if ($(this).val() !== "") {
				$(this).watermark($(this).attr("value"));
			} 
			else if ($(this).attr("title") !== ""){
				$(this).watermark($(this).attr("title"));
			}
		});
		
		//set default text for password fields from 
		$("input[type='password']").each(function(){
			$(this).watermark($(this).attr("title"));
		});
		
	},
	SlideShow: function(){
		// temp jquery.cycle.lite.1.0.min.js
		(function(D){var A="Lite-1.0";D.fn.cycle=function(E){return this.each(function(){E=E||{};if(this.cycleTimeout){clearTimeout(this.cycleTimeout)}this.cycleTimeout=0;this.cyclePause=0;var I=D(this);var J=E.slideExpr?D(E.slideExpr,this):I.children();var G=J.get();if(G.length<2){if(window.console&&window.console.log){window.console.log("terminating; too few slides: "+G.length)}return }var H=D.extend({},D.fn.cycle.defaults,E||{},D.metadata?I.metadata():D.meta?I.data():{});H.before=H.before?[H.before]:[];H.after=H.after?[H.after]:[];H.after.unshift(function(){H.busy=0});var F=this.className;H.width=parseInt((F.match(/w:(\d+)/)||[])[1])||H.width;H.height=parseInt((F.match(/h:(\d+)/)||[])[1])||H.height;H.timeout=parseInt((F.match(/t:(\d+)/)||[])[1])||H.timeout;if(I.css("position")=="static"){I.css("position","relative")}if(H.width){I.width(H.width)}if(H.height&&H.height!="auto"){I.height(H.height)}var K=0;J.css({position:"absolute",top:0,left:0}).hide().each(function(M){D(this).css("z-index",G.length-M)});D(G[K]).css("opacity",1).show();if(D.browser.msie){G[K].style.removeAttribute("filter")}if(H.fit&&H.width){J.width(H.width)}if(H.fit&&H.height&&H.height!="auto"){J.height(H.height)}if(H.pause){I.hover(function(){this.cyclePause=1},function(){this.cyclePause=0})}D.fn.cycle.transitions.fade(I,J,H);J.each(function(){var M=D(this);this.cycleH=(H.fit&&H.height)?H.height:M.height();this.cycleW=(H.fit&&H.width)?H.width:M.width()});J.not(":eq("+K+")").css({opacity:0});if(H.cssFirst){D(J[K]).css(H.cssFirst)}if(H.timeout){if(H.speed.constructor==String){H.speed={slow:600,fast:200}[H.speed]||400}if(!H.sync){H.speed=H.speed/2}while((H.timeout-H.speed)<250){H.timeout+=H.speed}}H.speedIn=H.speed;H.speedOut=H.speed;H.slideCount=G.length;H.currSlide=K;H.nextSlide=1;var L=J[K];if(H.before.length){H.before[0].apply(L,[L,L,H,true])}if(H.after.length>1){H.after[1].apply(L,[L,L,H,true])}if(H.click&&!H.next){H.next=H.click}if(H.next){D(H.next).bind("click",function(){return C(G,H,H.rev?-1:1)})}if(H.prev){D(H.prev).bind("click",function(){return C(G,H,H.rev?1:-1)})}if(H.timeout){this.cycleTimeout=setTimeout(function(){B(G,H,0,!H.rev)},H.timeout+(H.delay||0))}})};function B(J,E,I,K){if(E.busy){return }var H=J[0].parentNode,M=J[E.currSlide],L=J[E.nextSlide];if(H.cycleTimeout===0&&!I){return }if(I||!H.cyclePause){if(E.before.length){D.each(E.before,function(N,O){O.apply(L,[M,L,E,K])})}var F=function(){if(D.browser.msie){this.style.removeAttribute("filter")}D.each(E.after,function(N,O){O.apply(L,[M,L,E,K])})};if(E.nextSlide!=E.currSlide){E.busy=1;D.fn.cycle.custom(M,L,E,F)}var G=(E.nextSlide+1)==J.length;E.nextSlide=G?0:E.nextSlide+1;E.currSlide=G?J.length-1:E.nextSlide-1}if(E.timeout){H.cycleTimeout=setTimeout(function(){B(J,E,0,!E.rev)},E.timeout)}}function C(E,F,I){var H=E[0].parentNode,G=H.cycleTimeout;if(G){clearTimeout(G);H.cycleTimeout=0}F.nextSlide=F.currSlide+I;if(F.nextSlide<0){F.nextSlide=E.length-1}else{if(F.nextSlide>=E.length){F.nextSlide=0}}B(E,F,1,I>=0);return false}D.fn.cycle.custom=function(K,H,I,E){var J=D(K),G=D(H);G.css({opacity:0});var F=function(){G.animate({opacity:1},I.speedIn,I.easeIn,E)};J.animate({opacity:0},I.speedOut,I.easeOut,function(){J.css({display:"none"});if(!I.sync){F()}});if(I.sync){F()}};D.fn.cycle.transitions={fade:function(F,G,E){G.not(":eq(0)").css("opacity",0);E.before.push(function(){D(this).show()})}};D.fn.cycle.ver=function(){return A};D.fn.cycle.defaults={timeout:4000,speed:1000,next:null,prev:null,before:null,after:null,height:"auto",sync:1,fit:0,pause:0,delay:0,slideExpr:null}})(jQuery)
		
		//set slideShow
		$('.slideShow').cycle({
			timeout: 7000,  // milliseconds between slide transitions (0 to disable auto advance) 
			speed: 2000,  	// speed of the transition (any valid fx speed value) 
			pause: true		// true to enable "pause on hover"
		});
	},
	Rating: function(){
		$(".rating ul li").click(function(){
			//coloring stars
			$(this).parent().find("li").removeClass("on");
			$(this).addClass("on").prevAll().addClass("on");
			
			//write rating score in hidden input field
			$(this).parents(".rating").find("input.rankingScore").val($(this).prevAll().length + 1);
		});
	},
	Calendar: function(){
		
		var dateList = [""];
		dateList = ["2010-02-08", "2010-02-10", "2010-03-03", "2010-03-05", "2010-03-20", "2010-04-20"];
		
		$(".calendar").datepicker({
			dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
			showOtherMonths: true,
			selectOtherMonths: true,
			dateFormat: 'DD, MM dd, yy',
	  		minDate: $.datepicker.parseDate('yy-mm-dd', dateList), //this makes the datepicker start at the first available
	  		beforeShowDay: function(dateToShow){
	    		return [($.inArray($.datepicker.formatDate('yy-mm-dd', dateToShow),dateList) >= 0), "eventDay" , "This is tooltip for event day!"]; 
	  		},
			onSelect: function(dateText, inst) { 
					alert(dateText);
			}
		});		
	},
	init: function(){
		//NStage.Utils.Fonts();
		NStage.Utils.JsCheck();
		NStage.Utils.ClearDefaultVal();
		NStage.Utils.SlideShow();
		NStage.Utils.Rating();
		//NStage.Utils.Calendar();
	}
};

$(document).ready(function(){
	NStage.Utils.init();
});