jQuery(function(){
	styleSwitcher.init(true);
	jQuery("#themechanger").change(function(){
		styleSwitcher.init(false);
	});
	jQuery('a[rel="external"]').click(function() {
		var url = jQuery(this).attr('href');
		window.open(url,'_blank');
		return false;
	});
	jQuery('.lg-polls-answers input').click(function (event) {jQuery('.lg-poll form').submit();});
		if (jQuery('.lg-poll-results')) {jQuery('.lg-poll-results').hide();}
			jQuery('.poll_results_view').click(function (event) {
				if (jQuery(this).text() == 'See Poll Results') {
					jQuery(this).text('Hide Poll Results');
					jQuery('.lg-polls-questions').toggle('slow', function() {
	        	jQuery('.lg-poll-results').toggle('slow');
	        }
				);
			}
			else {
				jQuery(this).text('See Poll Results');
				jQuery('.lg-poll-results').toggle('slow', function() {
	     		jQuery('.lg-polls-questions').toggle('slow');
	      }
			);
		}
		return false;
	});
	
	scroller.getWidth(jQuery('#banners .wrap ul.scroller'), jQuery("#banners .wrap li"));
	jQuery('.pagination a.prev').hide();
	jQuery('#banners .wrap').serialScroll({
		items:'li',
		prev:'.pagination  a.prev',
		next:'.pagination  a.next',
		axis:'xy',
		navigation:'.pagination li a',
		easing:'linear',
		duration:1200,
		constant: false,
		start: 0,	
		force: true,
		interval: 8000,
		step: 3,
		onBefore:function( e, elem, $pane, $items, pos ){
 			e.preventDefault();
 			if( this.blur )
 				this.blur();
			jQuery('.pagination li a').each(function(i){
				if(i == (pos/3)){
					jQuery('.pagination li a').not(jQuery(this)).removeAttr("class");
					jQuery(this).addClass("active");
				}
			});
 		},
		onAfter:function(elem){
			scroller.setup(elem, jQuery('#banners .wrap'), jQuery("#banners .wrap ul.scroller"), jQuery("#banners .wrap li"), jQuery('.pagination a.prev'), jQuery('.pagination a.next'));
		}
	});
	defaultText.init();
});

var styleSwitcher = {
	init: function(isInitialLoad){
		if(jQuery("#themechanger").length){
			 if(isInitialLoad && jQuery("link#theme").size() == 0){
				styleSwitcher.createStyleSheet(styleSwitcher.getFormSrc(1));	
				styleSwitcher.setStyleCookie(styleSwitcher.getFormSrc(1));
				}else if(isInitialLoad && jQuery("link#theme").size() > 0){
					return false;
				}else{
					if(jQuery("link#theme").size() == 0){
							styleSwitcher.createStyleSheet(styleSwitcher.getFormSrc());	
							styleSwitcher.setStyleCookie(styleSwitcher.getFormSrc());
					}else{
						if(jQuery("#themechanger option:selected").val() != jQuery("#themechanger option:eq(0)").val()){
							styleSwitcher.changeStyleSheet(styleSwitcher.getFormSrc());
							styleSwitcher.setStyleCookie(styleSwitcher.getFormSrc());
						}else{
							return false;
						}
					}
				}
			}
	},
	getFormSrc: function(index){
		var src = null;
		if(index != null){
			var elem = jQuery("#themechanger option").get(index);
			src = jQuery(elem).attr("title");
		}else{
			src = jQuery("#themechanger option:selected").attr("title");
		}
		return src;
	},
	cssPath: "/css/",
	cssExtension: ".css",
	changeStyleSheet: function(src){
		jQuery("link[id = 'global']").next().attr("href", styleSwitcher.cssPath + src + styleSwitcher.cssExtension);
	},
	createStyleSheet: function(src){
		var stylesheet = jQuery("<link />").attr({
			type: "text/css",
			rel: "stylesheet",
			media: "screen",
			href: styleSwitcher.cssPath + src + styleSwitcher.cssExtension,
			id: 'theme'
		});
		jQuery("link[id = 'global']").after(stylesheet);
	},
	cookieName: "exp_style",
	cookieExpire: function(){
		var date = new Date();
		date.setTime(date.getTime() + (365 * 1000 * 60 * 60 * 24)) 
		return date;
	},
	setStyleCookie: function(styleName){
	  	jQuery.cookie(styleSwitcher.cookieName, styleName, {expires: styleSwitcher.cookieExpire()}, "/");
	},
	getStyleCookie: function(){
		var cookie = jQuery.cookie(styleSwitcher.cookieName);
		return cookie;
	}
}

var scroller = {
	scrollWidth: 0,
	setup : function(elem, scrollContainer,itemCollectionContainer, itemCollection, previous, next){

		if((scrollContainer.scrollLeft() + scrollContainer.width()) >= itemCollectionContainer.width()){
			jQuery(next).hide();
		}else{
			jQuery(next).show();
		}
		if(itemCollection.index(elem) == 0){
			jQuery(previous).hide();
		}else{
			jQuery(previous).show();
		}
	},
	getWidth : function(container, items){
		jQuery(items).each(function(){
	    scroller.scrollWidth += jQuery(this).width();
	  });
		jQuery(container).width(scroller.scrollWidth);
		scroller.scrollWidth = 0;
	}
}

var defaultText = {
	init : function() {
		jQuery(".defaultText").focus(function(srcc) {
			if (jQuery(this).val() == jQuery(this)[0].title) {
				jQuery(this).removeClass("defaultTextActive");
					jQuery(this).val("");
			}
		});
		jQuery(".defaultText").blur(function() {
			if (jQuery(this).val() == "") {
				jQuery(this).addClass("defaultTextActive");
				jQuery(this).val(jQuery(this)[0].title);
			}
		});
		jQuery(".defaultText").blur();
	}
};
