
/*******************************************************************************

  CSS on Sails Framework
  Title: Juice Analytics
  Author: XHTMLized (http://www.xhtmlized.com/)
  Date: October 2010

*******************************************************************************/

var $jQuery = jQuery.noConflict();

//1.2.5
(function($) {
	var t = $.tools.scrollable; 
	
	t.autoscroll = {
		conf: {
			autoplay: true,
			interval: 3000,
			autopause: true
		}
	};	
	
	// jQuery plugin implementation
	$.fn.autoscroll = function(conf) { 

		if (typeof conf == 'number') {
			conf = {interval: conf};	
		}
		
		var opts = $.extend({}, t.autoscroll.conf, conf), ret;
		
		this.each(function() {		
				
			var api = $(this).data("scrollable");			
			if (api) { ret = api; }
			
			// interval stuff
			var timer, stopped = true;
	
			api.play = function() { 
				
				// do not start additional timer if already exists
				if (timer) { return; }
				
				stopped = false;
				
				// construct new timer
				timer = setInterval(function() { 
					api.next();				
				}, opts.interval);
				
			};	

			api.pause = function() {
				timer = clearInterval(timer);
			};
			
			// when stopped - mouseover won't restart 
			api.stop = function() {
				api.pause();
				stopped = true;	
			};
			
			api.getRoot().add(api.getNaviButtons()).hover(function() {
				if (!stopped) {
					api.pause();
				}
			}, function() {
				if (!stopped) {
					api.play()
				}
			});
			
			if (opts.autoplay) {
				api.play();				
			}

		});
		
		return opts.api ? ret : this;
		
	}; 
	
})(jQuery);


$jQuery(document).ready(function() {

	//
	$jQuery('a[rel=_blank]').click(function(){
		this.target = "_blank";
	});

	// tabs
	$jQuery(".blog #sidebar .tabs .tab").fadeOut();
	$jQuery(".blog #sidebar .tabs.tab1active .tab1").fadeIn();
	$jQuery(".blog #sidebar .tabs.tab2active .tab2").fadeIn();
	$jQuery(".blog #sidebar .tabs .tab .h3 ul li a").click(function () {
		if ($jQuery(this).parent().parent().parent().parent().hasClass('tab1')) {
			$jQuery(".blog #sidebar .tabs .tab1").hide();
			$jQuery(".blog #sidebar .tabs .tab2").fadeIn();
			$jQuery(".blog #sidebar .tabs").removeClass('tab1active');
			$jQuery(".blog #sidebar .tabs").addClass('tab2active');
		}

		if ($jQuery(this).parent().parent().parent().parent().hasClass('tab2')) {
			$jQuery(".blog #sidebar .tabs .tab2").hide();
			$jQuery(".blog #sidebar .tabs .tab1").fadeIn();
			$jQuery(".blog #sidebar .tabs").removeClass('tab2active');
			$jQuery(".blog #sidebar .tabs").addClass('tab1active');
		}
		return false;
	});

	//
	$jQuery('#navigation').animate({'height': '54px'}, 10);

	$jQuery('#navigation ul').fadeOut();
	$jQuery('#navigation').hoverIntent(function() {
		$jQuery(this).animate({'height': '158px'}, 350);
		$jQuery('#navigation ul').fadeIn();
	}, function() {
		$jQuery(this).animate({'height': '54px'}, 350);
		$jQuery('#navigation ul').fadeOut();
	});

	//
		$jQuery('#header form fieldset input[type="text"]').animate({
			'width': '90px',
			//'padding': '0 0 0 8px',
			'font-size': '13px',
			//'font-style': 'italic',
			'color': '#909090',
			'background-position': '100% 0'
		}, 450);
	$jQuery('#header form fieldset input[type="text"]').focus(function() {
		$jQuery('#header form fieldset input[type="text"]').animate({
			'width': '138px',
			//'padding': '0 0 0 10px',
			'font-size': '11px',
			//'font-style': 'normal',
			'color': '#fff',
			'background-position': '100% -25px'
		}, 450);
	});
	$jQuery('#header form fieldset input[type="text"]').blur(function() {
		$jQuery('#header form fieldset input[type="text"]').animate({
			'width': '90px',
			//'padding': '0 0 0 8px',
			'font-size': '13px',
			//'font-style': 'italic',
			'color': '#909090',
			'background-position': '100% 0'
		}, 450);
	});


	//
	$jQuery('#header form fieldset input[type="text"], .contact-wrapper form fieldset input[type="text"], .login-wrapper form fieldset input[type="text"], .contact-wrapper form fieldset textarea').each(function() {
		var defaultValue = this.value;
		$jQuery(this).focus(function() {
			if(this.value == defaultValue) {
				this.value = '';
			}
		});
		$jQuery(this).blur(function() {
			if(this.value == '') {
				this.value = defaultValue;
			} else {
			}
		});
	});

	//simply form validatiom - login
	$jQuery('.login-wrapper form').submit(function() {
		var field1 = $jQuery('.login-wrapper form input[type="text"]');
		var field1text = $jQuery('.login-wrapper form input[type="text"]').attr('value');
		var field2 = $jQuery('.login-wrapper form input[type="password"]');
		var field2length = $jQuery('.login-wrapper form input[type="password"]').attr('value').length;
		//console.log(field1text, field2length)
		if ( field1text == 'Name' || field1text == '') {
			$jQuery(field1).addClass('error');
		} else {
			$jQuery(field1).removeClass('error');
		}
		if ( field2length == 0 ) {
			$jQuery(field2).addClass('error');
		} else {
			$jQuery(field2).removeClass('error');
		}
		if ( field1text == 'Name' || field1text == '' || field2length == 0 ) {
			$jQuery('.login-wrapper form fieldset .error-info').fadeIn();
			return false;
		}
	});

	//simply form validatiom - contact
	$jQuery('.contact-wrapper form').submit(function() {
		var field1 = $jQuery('.contact-wrapper form input[type="text"]:first-child');
		var field1text = $jQuery('.contact-wrapper form input[type="text"]:first-child').attr('value');
		var field2 = $jQuery('.contact-wrapper form input[type="text"]:nth-child(2)');
		var field2text = $jQuery('.contact-wrapper form input[type="text"]:nth-child(2)').attr('value');
		//console.log(field1text, field2text)
		if ( field1text == 'Name' || field1text == '') {
			$jQuery(field1).addClass('error');
		} else {
			$jQuery(field1).removeClass('error');
		}
		if ( field2text == 'Email Address' || field2text == '') {
			$jQuery(field2).addClass('error');
		} else {
			$jQuery(field2).removeClass('error');
		}
		if ( field1text == 'Name' || field2text == 'Email Address' || field1text == '' ) {
			$jQuery('.contact-wrapper form fieldset .error-info').fadeIn();
			return false;
		}
	});

	//


	function rebind(obj){
		obj.bind('click', sectionToggle);
	}

	function sectionToggle(event){
		var obj = $jQuery(this),
				section = $jQuery(this).closest('.section');

		obj.unbind(event);
		setTimeout(function(){rebind(obj);}, 360);
		
		if(section.hasClass('active')) {
			if(section.hasClass('section1')) {
				$jQuery('.about-wrapper .section .header > p').hide();
				$jQuery(".about-wrapper .section.section1 .content.active ul li").unbind('mouseover', hoverOn).unbind('mouseout', hoverOff);
			}
			section.switchClass('active', '', 350);
		}else {
			if ($jQuery(this).closest('.section').hasClass('section1')) {
				$jQuery('.about-wrapper .section .header > p').fadeIn();
				$jQuery(".about-wrapper .section.section1 .content.active ul li").bind('mouseover', hoverOn).bind('mouseout', hoverOff);
			}
			section.switchClass('', 'active', 350);
		}
	}
	
	$jQuery('.about-wrapper .section .header .h3').each(function(){$jQuery(this).bind('click', sectionToggle)});

	//
	$jQuery('.about-wrapper .section .header .person-info').hide();
	$jQuery('.about-wrapper .section.section1 .content.active ul li img').each(function() {
		var bg = $jQuery(this).attr('src');
		var newBg = bg.replace('.jpg','');
		$jQuery(this).parent().css({'background':'url('+newBg+'_hover.jpg) no-repeat'});
	});
	
	//
	function hoverOn () {
		$jQuery('.about-wrapper .section.section1 .content ul li').removeClass('active');
		$jQuery('.about-wrapper .section .header > p').hide();
		$jQuery('.about-wrapper .section .header .person-info').hide();
		var className = $jQuery(this).attr('class');
		if($jQuery(this).closest('.section').hasClass('active')) {
			$jQuery('.about-wrapper .section .header .person-info.'+className).show();
		}
	}
	
	function hoverOff () {
		$jQuery('.about-wrapper .section .header .person-info').hide();
		if($jQuery(this).closest('.section').hasClass('active')) {
			$jQuery('.about-wrapper .section .header > p').fadeIn(200);
		}
	}

	//
	$jQuery(".about-wrapper .section.section2 .content .slideshow").scrollable({ circular: true, mousewheel: true }).navigator({
		navi: ".about-wrapper .section.section2 .content .slideshow-nav li.navi",
		naviItem: 'span',
		activeClass: 'current',
		prev: '.about-wrapper .section.section2 .content .slideshow-nav li.prev',
		next: '.about-wrapper .section.section2 .content .slideshow-nav li.next'
	});

	//
	$jQuery(".about-wrapper .section.section3 .content .slideshow").scrollable({ circular: true, mousewheel: true }).navigator({
		navi: ".about-wrapper .section.section3 .content .slideshow-nav li.navi",
		naviItem: 'span',
		activeClass: 'current',
		prev: '.about-wrapper .section.section3 .content .slideshow-nav li.prev',
		next: '.about-wrapper .section.section3 .content .slideshow-nav li.next'
	}).autoscroll({ interval: 10000, autoplay: true, autopause: true });

	//
	var api_2 = $jQuery(".home-bottom .slideshow-wrapper .slideshow").scrollable({
		circular: true 
	}).navigator({
		navi: ".home-bottom .slideshow-wrapper ul",
		naviItem: 'li',
		activeClass: 'active'
	}).autoscroll({ 
		interval: 10000,
		autoplay: false,
		api: true
	});
	
	//
	$jQuery('.home-bottom .slideshow-wrapper .play-pause').click(function(){
		if($jQuery(this).hasClass('pause')){
			$jQuery(this).removeClass('pause');
			$jQuery(this).addClass('play');
			api_2.stop();
		}
		else{
			$jQuery(this).removeClass('play');
			$jQuery(this).addClass('pause');
			api_2.play();
		}
	});


	//
	$jQuery('.login-wrapper form fieldset input[type="password"]').hashmask();
	
	var mockpass = $jQuery('<input type="text" value="Password" />');
	var realpass = $jQuery('.login-wrapper form fieldset input[type="password"]');
	var sparkline = $jQuery('#-jquery-hashmask-sparkline');
	realpass.hide();
	sparkline.hide();
	realpass.after(mockpass);

	mockpass.focus(function() {
		mockpass.hide();
		realpass.show();
		sparkline.show();
		realpass.focus();
	});
	
	realpass.blur(function() {
		if (realpass.val() == '') {
			mockpass.show();
			realpass.hide();
			sparkline.hide();
		}
	});

	//
	if (($jQuery.browser.mozilla)){

		//
		$jQuery('html').addClass('firefox');

	}

	//
	if (($jQuery.browser.opera)){

		//
		$jQuery('html').addClass('opera');

	}

	//
	if (($jQuery.browser.msie) && ($jQuery.browser.version <= "8.0")){

	}

	//
	if (($jQuery.browser.msie) && ($jQuery.browser.version == "8.0")){

		//
		$jQuery('html').addClass('ie8');

	}

	//
	if (($jQuery.browser.msie) && ($jQuery.browser.version == "7.0")){

		//
		$jQuery('html').addClass('ie7');

	}

});

$jQuery(window).load(function() {

	if ( $jQuery('body').hasClass('aboutus') ) {
		// random
		/*
		var randomNumLib1 = $jQuery(".about-wrapper .section.section2 .content .slideshow-nav li.navi span").size();
		var randomNum1 = Math.floor ( Math.random() * randomNumLib1 ) ;
		*/
		$jQuery('.about-wrapper .section.section2 .content .slideshow-nav li.navi span:nth-child(1)').click();

		// random
		/*
		var randomNumLib2 = $jQuery(".about-wrapper .section.section3 .content .slideshow-nav li.navi span").size();
		var randomNum2 = Math.floor ( Math.random() * randomNumLib2 ) ;
		*/
		$jQuery('.about-wrapper .section.section3 .content .slideshow-nav li.navi span:nth-child(1)').click();

		// random
		/*
		var randomNumLib3 = $jQuery(".about-wrapper .section.section1 .content.active ul li").size();
		var randomNum3 = Math.floor ( Math.random() * randomNumLib3 ) ;
		$jQuery('.about-wrapper .section.section1 .content.active ul li:nth-child('+randomNum3+')').addClass('active');
		var random3Class = $jQuery('.about-wrapper .section.section1 .content.active ul li:nth-child('+randomNum3+')').attr('class');
		var random3ClassOut = random3Class.replace(' active','');
		$jQuery('.about-wrapper .section .header > p').hide();
		$jQuery('.about-wrapper .section .header .person-info.'+random3ClassOut).fadeIn();
		*/

	}

	if ( $jQuery('body').hasClass('home') ) {
		// random
		var randomNumLib4 = $jQuery(".home-bottom .slideshow-wrapper ul li").size();
		var randomNum4 = 1 + Math.floor ( Math.random() * randomNumLib4 ) ;
		$jQuery('.home-bottom .slideshow-wrapper ul li:nth-child('+randomNum4+')').click();
		$jQuery('.home-bottom .slideshow-wrapper .play-pause').click();
	}

});
