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

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

$(document).ready(function(){ 

	$('#content_wrapper .inner .card a[rel=slide]').fancybox({ 
		padding: 0,
		overlayColor: '#000', 
		overlayOpacity: .8
	});
	
	$('.gallery_vimeo').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		overlayOpacity: .8
	});
	
	$('.gallery_youtube').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		overlayOpacity: .8
	});
	
	$('.img_frame').fancybox({ 
		padding: 0,
		overlayColor: '#000', 
		overlayOpacity: .8
	});
	
	$.validator.setDefaults({
		submitHandler: function() { 
		    var actionUrl = $('#contact_form').attr('action');
		    
		    $.ajax({
  		    	type: 'POST',
  		    	url: actionUrl,
  		    	data: $('#contact_form').serialize(),
  		    	success: function(msg){
  		    		$('#contact_form').hide();
  		    		$('#reponse_msg').html(msg);
  		    	}
		    });
		    
		    return false;
		}
	});
		    
		
	$('#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"
		}
	});	
	
	var photoItems = $('#content_wrapper .inner .card').length;
	var photoWidth = 424; 
	var scrollArea = photoWidth * photoItems;
	var scrollWidth = $('#wrapper').width() - 235;
	
	$('#content_wrapper').css({width: scrollWidth+'px'});
	
	$(window).resize(function() {
		var scrollWidth = $('#wrapper').width() - 235;
  		$('#content_wrapper').css({width: scrollWidth+'px'});
	});

	
	$("#content_wrapper .inner").css('width', scrollArea);
	$("#content_wrapper").attr({scrollLeft: 0});					   
	
	$("#content_wrapper").css({"overflow":"hidden"});
	
	$("#move_next").mouseenter( 
    	function() {
        	timerId = setInterval(function() { 
        	
        		var speed = 5;
				var slider = $('#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);
    	}
    );
    $("#move_next").mouseleave( 
    	function() { 
    		clearInterval(timerId); 
    	}
	);
	
	$("#move_prev").mouseenter(
    	function() {
        	timerId = setInterval(function() { 
        	
        		var speed = 5;
				var slider = $('#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);
    	}
    );
    $("#move_prev").mouseleave(
    	function() { 
    		clearInterval(timerId); 
    	}
	);
	
	$('#content_slider').slider({
		animate: 'slow',
		change: changeSlide,
		slide: doSlide
	});
	
	
	$("##content_wrapper .inner .card").hover(
    	function() {
    		$(this).find('.title').css({ top: -($(this).find('.title').height()+35)});
        	$(this).find('.title').animate({ opacity: .8 }, 300);
    	},
    	function() { 
    		$(this).find('.title').animate({ opacity: 0 }, 0);
    	}
	);
	
	function changeSlide(e, ui)
	{
		var maxScroll = $("#content_wrapper").attr("scrollWidth") - $("#content_wrapper").width();
		var currentScroll = (ui.value * (maxScroll / 100))-65;
		$("#content_wrapper").stop().animate({scrollLeft: currentScroll}, 1200);
	}

	function doSlide(e, ui)
	{
		var maxScroll = $("#content_wrapper").attr("scrollWidth") - $("#content_wrapper").width();
		var currentScroll = (ui.value * (maxScroll / 100))-65;
		$("#content_wrapper").stop().attr({scrollLeft: currentScroll});
	}
	
	$('#main_menu li:not(.current_page_item)').each(function()
	{	
		
		$(this).hover(function()
		{	
			$(this).find('a').addClass('hover');
			$(this).find('a').animate({left: 5}, 200);
		},
		function()
		{	
			$(this).find('a').removeClass('hover');
			$(this).find('a').animate({left: 0}, 200);
		});	
		
	});

});
