

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,div: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,div: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(document).ready(function(){ 

	$j(document).setNav();
	
	$j('.img_frame').fancybox({ 
		padding: 10,
		overlayColor: '#000',
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .7
	});
	
	$j('.pp_gallery a').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .7
	});
	
	$j('.flickr li a').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .7
	});
	
	$j('.lightbox').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .7
	});
	
	$j('.lightbox_youtube').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .7
	});
	
	$j('.lightbox_vimeo').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .7
	});
	
	$j('.lightbox_dailymotion').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .7
	});
	
	$j('.lightbox_iframe').fancybox({ 
		padding: 10,
		type: 'iframe',
		overlayColor: '#000', 
		transitionIn: 'fade',
		transitionOut: 'fade',
		overlayOpacity: .7,
		width: 900,
		height: 650
	});
	
	$j('a[rel=gallery]').fancybox({ 
		padding: 10,
		overlayColor: '#000', 
		overlayOpacity: .7
	});
	
	if($j.browser.msie && $j.browser.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);
	}
	
	
	
	if(!$j.browser.msie)
 	{
		$j('#slider_wrapper').hover(function(){  
				$j('.nivo-controlNav').fadeIn();
 			}  
  			, function(){  
  				$j('.nivo-controlNav').fadeOut();
  			}  
  			
		);
		
		$j('.nivo_border').hover(function(){  
				$j('.nivo-controlNav').fadeIn();
 			}  
  			, function(){  
  				$j('.nivo-controlNav').fadeOut();
  			}  
  			
		);
	}
	else
	{
		$j('#slider_wrapper').hover(function(){  
				$j('.nivo-controlNav').show();
 			}  
  			, function(){  
  				$j('.nivo-controlNav').hide();
  			}  
  			
		);
		
		$j('.nivo_border').hover(function(){  
				$j('.nivo-controlNav').show();
 			}  
  			, function(){  
  				$j('.nivo-controlNav').hide();
  			}  
  			
		);
	}
	
	
	
	
	
	var footerLi = 0;
	$j('#footer .sidebar_widget li.widget').each(function()
	{
		footerLi++;
		
		if(footerLi%$j('#pp_footer_style').val() == 0)
		{ 
			$j(this).addClass('last');
		}
	});
	
	
	
	$j('.home_portfolio img.frame').each(function()
	{
		$j(this).hover(function()
		{	
			$j(this).animate({top: '-10px'}, 300);
		},
		function()
		{	
			$j(this).animate({top: 0}, 300);
		});	
	});
	
	
	
	
	
	$j('.tagline').css('visibility', 'visible');
	
	// Clone applications to get a second collection
	var $jdata = $j(".portfolio-content").clone();
	var pp_portfolio_sorting = $j('#pp_portfolio_sorting').val();

	$j('.portfolio-main li').click(function(e) {
	
		$j(".filter li").removeClass("active");	
		// Use the last category class as the category to filter by. This means that multiple categories are not supported (yet)
		var filterClass=$j(this).attr('class').split(' ').slice(-1)[0];
		
		if (filterClass == 'all-projects') {
			var $jfilteredData = $jdata.find('.project');
		} else {
			var $jfilteredData = $jdata.find('.project[data-type~=' + filterClass + ']');
		}
		$j(".portfolio-content").quicksand($jfilteredData, {
			duration: 600,
			easing: pp_portfolio_sorting,
			useScaling: false,
    		enhancement: function() {
    			$j('.img_frame').fancybox({ 
					padding: 10,
					overlayColor: '#000',
					transitionIn: 'fade',
					transitionOut: 'fade',
					overlayOpacity: .7
				});
				
				$j('.lightbox_youtube').fancybox({ 
					padding: 10,
					overlayColor: '#000', 
					transitionIn: 'fade',
					transitionOut: 'fade',
					overlayOpacity: .7
				});
				
				$j('.lightbox_vimeo').fancybox({ 
					padding: 10,
					overlayColor: '#000', 
					transitionIn: 'fade',
					transitionOut: 'fade',
					overlayOpacity: .7
				});
    		
      			$j('.img_nofade').hover(function(){  
						$j(this).animate({boxShadow: '0 1px 5px #1F7099'});
 					}  
  					, function(){  
  						$j(this).animate({boxShadow: '0 0 0 #ccc'});
  					}  
  					
				);
    		}
		});	
		$j(this).addClass("active"); 
					
		return false;
	});
	
	$j('#option_btn').click(
    	function() {
    		if($j('#option_wrapper').css('left') != '0px')
    		{
 				$j('#option_wrapper').animate({"left": "0px"}, { duration: 300 });
	 			$j(this).animate({"left": "140px"}, { duration: 300 });
	 		}
	 		else
	 		{
	 			$j('#option_wrapper').animate({"left": "-140px"}, { duration: 300 });
    			$j('#option_btn').animate({"left": "0px"}, { duration: 300 });
	 		}
    	}
    );
    
   
    
    	
	$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"
		}
	});	
	
	
    
    $j('pre').each(function()
	{
		preContent = $j(this).html();
	});
	
	$j('#pp_bg_pattern').change(function(){ 
 		pp_pattern = $j(this).val();
 	
 		$j.ajax({
  			type: 'GET',
  			url: $j('#form_option').attr('action'),
  			data: 'pp_bg_pattern='+$j(this).val(),
  			success: function(){
   				if(pp_pattern == '')
				{
					location.href = location.href;
				}
  			}
		});
		
		$j('#header_pattern').attr('class', '');
		$j('#header_pattern').addClass($j(this).val());
		$j('#footer_pattern').attr('class', '');
		$j('#footer_pattern').addClass($j(this).val());
		
 	});
 	
 	$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>');
		}
	});/**/
 	
 	
 	
 	if($j.browser.msie && $j.browser.version > 7)
	{
 		bodyHeight = $j('body').height();
 		documentHeight = $j(document).height();
 		
 		if(bodyHeight < documentHeight)
 		{
 			expandHeight = documentHeight - bodyHeight;
 			footerHeight = $j('#footer').height()+expandHeight;
 			$j('#footer').height(footerHeight);
 			$j('#footer_pattern').css('marginTop', -footerHeight-20);
 			$j('#footer_pattern').height(footerHeight);
 			$j('#copyright').css('marginTop', expandHeight);
 		}
 		else
 		{
 			footerHeight = $j('#footer').height()+20;
 			$j('#footer_pattern').css('marginTop', -footerHeight);
 			$j('#footer_pattern').height(footerHeight);
 		}
 	}
 	else if(!$j.browser.msie)
 	{
 		bodyHeight = $j('body').height();
 		documentHeight = $j(document).height();
 		
 		if(bodyHeight < documentHeight)
 		{
 			expandHeight = documentHeight - bodyHeight;
 			footerHeight = $j('#footer').height()+expandHeight;
 			$j('#footer').height(footerHeight);
 			$j('#footer_pattern').css('marginTop', -footerHeight-20);
 			$j('#footer_pattern').height(footerHeight);
 			$j('#copyright').css('marginTop', expandHeight);
 		}
 		else
 		{
 			footerHeight = $j('#footer').height()+20;
 			$j('#footer_pattern').css('marginTop', -footerHeight);
 			$j('#footer_pattern').height(footerHeight);
 		}
 	}
	
});
