/*
	Easy plugin to get element index position
	Author: Peerapong Pulpipatnan
	http://themeforest.net/user/peerapong
*/

var $j = jQuery.noConflict();

$j.fn.getIndex = function(){
	var $jp=$j(this).parent().children();
    return $jp.index(this);
}

$j.fn.setNav = function(){
	$j('#main_menu li ul').css({display: 'none'});

	$j('#main_menu li').each(function()
	{	
		var $jsublist = $j(this).find('ul:first');
		
		$j(this).hover(function()
		{	
			$jsublist.css({opacity: 1});
			
			$jsublist.stop().css({overflow:'hidden', height:'auto', display:'none'}).fadeIn(200, function()
			{
				$j(this).css({overflow:'visible', height:'auto', display: 'block'});
			});	
		},
		function()
		{	
			$jsublist.stop().css({overflow:'hidden', height:'auto', display:'none'}).fadeOut(200, function()
			{
				$j(this).css({overflow:'hidden', display:'none'});
			});	
		});

	});
	
	$j('#main_menu li').each(function()
	{
		
		$j(this).hover(function()
		{	
			$j(this).find('a:first').addClass('hover');
		},
		function()
		{	
			$j(this).find('a:first').removeClass('hover');
		});	
		
	});
	
	$j('#menu_wrapper .nav ul li ul').css({display: 'none'});

	$j('#menu_wrapper .nav ul li').each(function()
	{	
		
		var $jsublist = $j(this).find('ul:first');
		
		$j(this).hover(function()
		{	
			$jsublist.css({opacity: 1});
			
			$jsublist.stop().css({overflow:'hidden', height:'auto', display:'none'}).fadeIn(200, function()
			{
				$j(this).css({overflow:'visible', height:'auto', display: 'block'});
			});	
		},
		function()
		{	
			$jsublist.stop().css({overflow:'hidden', height:'auto', display:'none'}).fadeOut(200, function()
			{
				$j(this).css({overflow:'hidden', display:'none'});
			});	
		});	
		
	});
	
	$j('#menu_wrapper .nav ul li').each(function()
	{
		
		$j(this).hover(function()
		{	
			$j(this).find('a:first').addClass('hover');
		},
		function()
		{	
			$j(this).find('a:first').removeClass('hover');
		});	
		
	});
}

$j(function () {

    	$j('.slideshow').anythingSlider({
    	        easing: "easeInOutExpo",
    	        autoPlay: false,
    	        startStopped: false,
    	        animationTime: 600,
    	        hashTags: false,
    	        buildNavigation: true,
    	        buildArrows: false,
    			pauseOnHover: true,
    			startText: "Go",
    	        stopText: "Stop"
    	    });
    	    
    });

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

	//$j(document).setNav();
	
	$j('#content_wrapper').show();
	
	$j('.img_frame').fancybox({ 
		padding: 10,
		overlayColor: '#000',
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .8
	});
	
	$j('.pp_gallery a').fancybox({ 
		padding: 0,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .8
	});
	
	$j('.flickr li a').fancybox({ 
		padding: 0,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .8
	});
	
	$j('.lightbox').fancybox({ 
		padding: 0,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .8
	});
	
	$j('.lightbox_youtube').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .8
	});
	
	$j('.lightbox_vimeo').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .8
	});
	
	$j('.lightbox_dailymotion').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .8
	});
	
	$j('.lightbox_iframe').fancybox({ 
		padding: 0,
		type: 'iframe',
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .8,
		width: 900,
		height: 650
	});
	
	$j('#content_wrapper .inner .card a.img_link').fancybox({ 
		padding: 0,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .8
	});

	$j.validator.setDefaults({
		submitHandler: function() { 
		    var actionUrl = $j('#contact_form').attr('action');
		    
		    $j.ajax({
  		    	type: 'GET',
  		    	url: actionUrl,
  		    	data: $j('#contact_form').serialize(),
  		    	success: function(msg){
  		    		$j('#contact_form').hide();
  		    		$j('#reponse_msg').html(msg);
  		    	}
		    });
		    
		    return false;
		}
	});
		    
		
	$j('#contact_form').validate({
		rules: {
		    your_name: "required",
		    email: {
		    	required: true,
		    	email: true
		    },
		    message: "required"
		},
		messages: {
		    your_name: "Please enter your name",
		    email: "Please enter a valid email address",
		    agree: "Please enter some message"
		}
	});	
	
	if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 8)
	{
		var zIndexNumber = 1000;
		$j('div').each(function() {
			$j(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});

		$j('#thumbNav').css('zIndex', 1000);
		$j('#thumbLeftNav').css('zIndex', 1000);
		$j('#thumbRightNav').css('zIndex', 1000);
		$j('#fancybox-wrap').css('zIndex', 1001);
		$j('#fancybox-overlay').css('zIndex', 1000);
	}
	
	$j('.portfolio2_hover').hide();
	$j('.one_half .portfolio_image').hover(function(){  
 			$j(this).find('.portfolio2_hover').show();
 			$j(this).find('img').animate({top: '11px'}, 300);
 			
 			$j(this).click(function(){
 				$j(this).find('a').click();
 			});
 		}  
  		, function(){  
  		
  			$j(this).find('img').animate({top: '32px'}, 300);
  			$j(this).find('.portfolio2_hover').hide();
  		}  
  		
	);
	
	$j('.portfolio3_hover').hide();
	$j('.one_third .portfolio_image').hover(function(){  
 			$j(this).find('.portfolio3_hover').show();
 			$j(this).find('img').animate({top: '20px'}, 300);
 			
 			$j(this).click(function(){
 				$j(this).find('a').click();
 			});
 		} 
  		, function(){  
  		
  			$j(this).find('img').animate({top: '30px'}, 300);
  			$j(this).find('.portfolio3_hover').hide();
  		}  
  		
	);
	
	$j('.portfolio4_hover').hide();
	$j('.one_fourth .portfolio_image').hover(function(){  
 			$j(this).find('.portfolio4_hover').show();
 			$j(this).find('img').animate({top: '3px'}, 300);
 			
 			$j(this).click(function(){
 				$j(this).find('a').click();
 			});
 		}  
  		, function(){  
  		
  			$j(this).find('img').animate({top: '20px'}, 300);
  			$j(this).find('.portfolio4_hover').hide();
  		}  
  		
	);
	
	$j('.post_img').hover(function(){  
 			$j(this).find('img').animate({top: '0px'}, 300);
 			
 			$j(this).click(function(){
 				$j(this).find('a').click();
 			});
 		}  
  		, function(){  
  		
  			$j(this).find('img').animate({top: '20px'}, 300);
  		}  
  		
	);
	
	$j('ul.flickr li img').hover(function(){  
 			$j(this).animate({top: '-5px'}, 300);
 		}  
  		, function(){  
  		
  			$j(this).animate({top: '0px'}, 300);
  		}  
  		
	);
	
	$j('.pp_gallery a img').hover(function(){  
 			$j(this).animate({top: '-3px'}, 100);
 		}  
  		, function(){  
  		
  			$j(this).animate({top: '0px'}, 100);
  		}  
  		
	);
	
	$j('.home_portfolio_grid').hover(function(){  
 			$j(this).animate({top: '-10px'}, 300);
 		}  
  		, function(){  
  		
  			$j(this).animate({top: '0px'}, 300);
  		}  
  		
	);
	
	$j('.card_portfolio_grid').hover(function(){  
 			$j(this).animate({top: '-10px'}, 300);
 		}  
  		, function(){  
  		
  			$j(this).animate({top: '0px'}, 300);
  		}  
  		
	);
	
	Cufon.replace('#welcome_msg');
	/*Cufon.replace('h1');
        Cufon.replace('h2');
        Cufon.replace('h3');
        Cufon.replace('h4');
        Cufon.replace('h5');*/
        Cufon.replace('h1.cufon');
	Cufon.replace('h1.cufon');
	Cufon.replace('h2.cufon');
	Cufon.replace('h2.quote');
	Cufon.replace('h2.widgettitle');
	Cufon.replace('h3.cufon');
	Cufon.replace('h4.cufon');
	Cufon.replace('h5.cufon');
	Cufon.replace('h6.cufon');
	Cufon.replace('.pricing_box h2');
	Cufon.replace('.pricing_box .header span');
	Cufon.replace('#searchform label');
	Cufon.replace('.tagline p');
	Cufon.replace('.dropcap1');
	Cufon.replace('.post_img_date');
	Cufon.replace('.page_caption h1.cufon');
	Cufon.replace('.page_caption h2.cufon');
	Cufon.replace('.ui-accordion-header');
	
	var footerLi = 0;
	$j('#footer .sidebar_widget li.widget').each(function()
	{
		footerLi++;
		
		if(footerLi%4 == 0)
		{ 
			$j(this).addClass('widget-four');
		}
	});
	
	VideoJS.setupAllWhenReady({
      controlsBelow: false, // Display control bar below video instead of in front of
      controlsHiding: true, // Hide controls when mouse is not over the video
      defaultVolume: 0.85, // Will be overridden by user's last volume if available
      flashVersion: 9, // Required flash version for fallback
      linksHiding: true // Hide download links when video is supported
    });
	
	$j('.home_portfolio img.frame').each(function()
	{
		$j(this).hover(function()
		{	
			$j(this).animate({top: '-15px'}, 300);
		},
		function()
		{	
			$j(this).animate({top: 0}, 300);
		});	
	});

	
	if(!isNaN($j('#slider_rows').val()))
	{
		var photoItems = $j('#content_wrapper .inner .card').length/$j('#slider_rows').val();
		var galleryWidth = $j('#gallery_width').val();
	}
	else
	{
		var photoItems = 1;
		var galleryWidth = 1;
	}

	if(Math.round(photoItems) < photoItems)
	{
		addItems = 1;
	}
	else
	{
		addItems = 0;	
	}
	
	if(!isNaN($j('#slider_rows').val()))
	{
		photoItems = parseInt(photoItems)+addItems; 
		var photoWidth = parseInt(galleryWidth)+2;  
		var scrollArea = parseInt(photoWidth*photoItems); 
		var scrollWidth = $j('#wrapper').width();
		
		$j('#content_wrapper').css({width: scrollWidth+'px'});
		
		$j(window).resize(function() {
			var scrollWidth = $j('#wrapper').width();
  			$j('#content_wrapper').css({width: scrollWidth+'px'});
		});
		
		
		$j("#content_wrapper .inner").css('width', scrollArea);
		$j("#content_wrapper").attr({scrollLeft: 0});					   
		
		$j("#content_wrapper").css({"overflow":"hidden"});
		
		
		var auto_scroll = $j('#pp_gallery_auto_scroll').val();
		
		$j("#move_prev").click(function(){
			var speed = parseInt($j('#slider_speed').val()*10);
			var slider = $j('#content_slider');
			var sliderCurrent = slider.slider("option", "value");
			sliderCurrent -= speed;
						
			if (sliderCurrent > slider.slider("option", "max")) sliderCurrent = slider.slider("option", "max");
			else if (sliderCurrent < slider.slider("option", "min")) sliderCurrent = slider.slider("option", "min");
						
			slider.slider("value", sliderCurrent);
		});
		
		$j("#move_next").click(function(){
			var speed = parseInt($j('#slider_speed').val()*10);
			var slider = $j('#content_slider');
			var sliderCurrent = slider.slider("option", "value");
			sliderCurrent += speed;
						
			if (sliderCurrent > slider.slider("option", "max")) sliderCurrent = slider.slider("option", "max");
			else if (sliderCurrent < slider.slider("option", "min")) sliderCurrent = slider.slider("option", "min");
						
			slider.slider("value", sliderCurrent);
		});
		
		if(auto_scroll != 0)
		{
			$j("#move_next").mouseenter( 
    			function() {
    		    	timerId = setInterval(function() { 
    		    	
    		    		var speed = parseInt($j('#slider_speed').val());
						var slider = $j('#content_slider');
						var sliderCurrent = slider.slider("option", "value");
						sliderCurrent += speed; // += and -= directions of scroling with MouseWheel
						
						if (sliderCurrent > slider.slider("option", "max")) sliderCurrent = slider.slider("option", "max");
						else if (sliderCurrent < slider.slider("option", "min")) sliderCurrent = slider.slider("option", "min");
						
						slider.slider("value", sliderCurrent);
    		    	
    		    	}, 100);
    		    	
    		    	//$(this).find('img').animate({ opacity: 1 }, 300);
    			}
    		);
    		$j("#move_next").mouseleave( 
    			function() { 
    				clearInterval(timerId); 
    			}
			);
			
			$j("#move_prev").mouseenter(
    			function() {
    		    	timerId = setInterval(function() { 
    		    	
    		    		var speed = parseInt($j('#slider_speed').val());
						var slider = $j('#content_slider');
						var sliderCurrent = slider.slider("option", "value");
						sliderCurrent -= speed; // += and -= directions of scroling with MouseWheel
						
						if (sliderCurrent > slider.slider("option", "max")) sliderCurrent = slider.slider("option", "max");
						else if (sliderCurrent < slider.slider("option", "min")) sliderCurrent = slider.slider("option", "min");
						
						slider.slider("value", sliderCurrent);
    		    	
    		    	}, 100);
    		    	
    		    	//$(this).find('img').animate({ opacity: 1 }, 300);
    			}
    		);
    		$j("#move_prev").mouseleave(
    			function() { 
    				clearInterval(timerId); 
    			}
			);
		}
		
		$j('#content_slider').slider({
			animate: 'slow',
			change: changeSlide,
			slide: doSlide
		});
	}
	
	function changeSlide(e, ui)
		{
			var maxScroll = $j("#content_wrapper").attr("scrollWidth") - $j("#content_wrapper").width();
			var currentScroll = (ui.value * (maxScroll / 100));
			$j("#content_wrapper").stop().animate({scrollLeft: currentScroll}, 1200);
		}
		
		function doSlide(e, ui)
		{
			var maxScroll = $j("#content_wrapper").attr("scrollWidth") - $j("#content_wrapper").width();
			var currentScroll = (ui.value * (maxScroll / 100));
			$j("#content_wrapper").stop().attr({scrollLeft: currentScroll});
		}
		
	$j('#welcome_msg').click(function(){
		$j(this).fadeOut();
	});
		
	$j('img.fade').hover(function(){  
 			$j(this).animate({opacity: 1}, 300);
 		}  
  		, function(){  
  		
  			$j(this).animate({opacity: $j('#img_opacity').val()}, 300);
  		}  
  		
	);
	
	$j('#slider_loading').hide();
	
	var slidePos = $j('#content_wrapper').position();
	var slideHeight = $j('#content_wrapper').height();
	
	arrowMidPos = slidePos.top+parseInt(slideHeight/2)-30;
	
	$j('#move_prev').css({display: 'block', top: arrowMidPos+'px'});
	$j('#move_next').css({display: 'block', top: arrowMidPos+'px'});
	
	var welcomeHeight = $j('#welcome_msg').height();
	welcomeMidYPos = slidePos.top+parseInt(slideHeight/2)-parseInt(welcomeHeight/2);
	$j('#welcome_msg').css({display: 'block', top: parseInt(welcomeMidYPos-20)+'px'});
	
	$j('#main_menu > li a').not("ul li ul a").each(function()
	{
		if($j(this).attr('title').length > 0)
		{
			$j(this).append('<span>'+$j(this).attr('title')+'</span>');
		}
	});
	
});