$(function () {

	//styleswitcher
	
	
	if(!($('body').hasClass('home')))
	
	{$('a.open').removeClass('open').addClass('close').css({ 'background' : 'black url(images/demo/ThemeChangerClosed.png) no-repeat center center' });
	
	$('#colorscheme').css({left: '-170px'});	
	
}
	
	$('a.open').live('click', function(){
		
		$(this).removeClass('open').addClass('close').css({ 'background' : 'black url(images/demo/ThemeChangerClosed.png) no-repeat center center' });
	
		$('#colorscheme').animate({left: '-=170'});	
		
		return false;

	}); 
	
	
	$('a.close').live('click', function(){
		
		$(this).removeClass('close').addClass('open').css({ 'background' : 'black url(images/demo/ThemeChangerOpen.png) no-repeat center center' });
	
		$('#colorscheme').animate({left: '+=170'});	
		
		return false;
				
	}); 
	
	$('#colorscheme a').click(function(){
		
		$('#colorscheme a').css('color', '#a8a8a8');
		
		$(this).css('color', 'white');
		
		
		
	}); 



	// CUFON FONT REPLACEMENT
    Cufon.replace('h1, h2, h3, h4, p.comment_author, .button');
	Cufon.replace('h1.logo a, #featured h2', {
	color: '-linear-gradient(0=#c5c5c5, 0.02=#f3f3f3, 0.53=#e1e1e1, 0.98=#ffffff, 1=#bfbfbf)'
	});


	// HOMEPAGE ILLUSTATION / SLIDER CHANGER
    if ($('body').hasClass('home')) {

        $('#slider_switch :checkbox').removeAttr('checked');
        $('#slider_switch :checkbox').iphoneStyle({
            checkedLabel: '&larr; Illustration',
            uncheckedLabel: 'Image slider &rarr;',
            resizeContainer: false,
            resizeHandle: false
        });

        $('#slider_switch input').change(function () {
            if ($(this).attr("checked")) {
                $('.featured_text').fadeOut();
                $('#featured_slider_home').css('visibility', 'visible').fadeIn();
                $('h2.slider_title').fadeIn();
                return false;
            }

            $('.featured_text').fadeIn();
            $('#featured_slider_home').fadeOut();
            $('h2.slider_title').fadeOut();
        });

    }
    
   
	//NAVBAR  SLIDEDOWN
    $('#navbar ul').css({
        display: "none"
    });
    
    $('#navbar li').hover(function () {
        $(this).find('ul:first').css({
            visibility: "visible",
            display: "none"
        }).slideDown('fast');
    }, function () {
        $(this).find('ul:first').css({
            visibility: "hidden"
        });
    });

    $("#navbar li").has('ul').addClass('parent_nav').find('>a').append('<span class="has_children"></span>');




	//GALLERY IMAGES HOVER SCRIPT
	
	$('#portfolio a.image_item').append('<span class="image_hover"></span>');	//add span to images 		
	$('#portfolio a.video_item').append('<span class="video_hover"></span>');	// add span to videos		

	$('#portfolio li a span:not(.new_work)').css('opacity', '0').css('display', 'block') //span opacity = 0 
	
	// show / hide span on hover
	$("#portfolio a").hover(
		 function () {
			 $(this).find('.image_hover, .video_hover').stop().fadeTo('slow', .7); }, 
		function () {
	  		  $('.image_hover, .video_hover').stop().fadeOut('slow', 0);
	});



	
	//HIDE FORM LABEL ON FOCUS
	
	 $('#contact_form input, #contact_form textarea, #comment_form input, #comment_form textarea, #newsletter_form input.form_input')
	.bind("focus.labelFx", function(){
		$(this).prev().fadeOut();
	})
	.bind("blur.labelFx", function(){
		$(this).prev()[!this.value ? "fadeIn" : "fadeOut"]();
	})
	.trigger("blur.labelFx");
		
		
	// CONTACT FORM HANDLING SCRIPT - WHEN USER CLICKS "SUBMIT"
	
	$("#contact_form #form_submit").click(function(){		
				   				 		
		
		// remove "error" class from text fields
		$("form input, form textarea").focus(function() {
 			$(this).removeClass('error_input');
 			$(this).parent().find('label.error').fadeOut().remove();
		});

		// set variables
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		
		// validate "name" field
		var nameVal = $("#form_name").val();
		if(nameVal == '') {
			$("#form_name")
			.after('<label for="form_name" class="error">Please enter your name</label>')
			.addClass('error_input')				  
			hasError = true;
			$("#form_name").parent().find('label:not(.error)').hide();	
		}
	
		// validate "e-mail" field - andd error message and animate border to red color on error
		var emailVal = $("#form_email").val();
		if(emailVal == '') {
			$("#form_email")
			.after('<label for="form_email" class="error">Please enter your e-mail</label>')
			.addClass('error_input')
			hasError = true; 
			
			$("#form_email").parent().find('label:not(.error)').hide();
				
		} else if(!emailReg.test(emailVal)) {	
			$("#form_email")
			.after('<label for="form_email" class="error">Please provide a valid e-mail</label>')
			.addClass('error_input')
			hasError = true;
			$("#form_email").attr("value", "");
			$("#form_email").parent().find('label:not(.error)').hide();
		}
		
				
		// validate "message" field
		var messageVal = $("#form_message").val();
		if(messageVal == '') {
			
			$("#form_message")
			.after('<label for="form_message" class="error comment_error">Please enter your message</label>')
			.addClass('error_input')
			hasError = true;
			
			$("#form_message").parent().find('label:not(.error)').hide();
		}
		
                // if the are errors - return false
                if(hasError == true) { return false; }
            
		// if no errors are found - submit the form with AJAX
		if(hasError == false) {
			
		var dataString = $('#contact_form').serialize();

			//hide the submit button and show the loader image	
			$("#form_submit").fadeOut('fast', function () {
			$('#loader').fadeIn(); 
			});
			       
			
		// make an Ajax request
        $.ajax({
            type: "POST",
            url: "php/contact-send.php",
            data: dataString,
            success: function(){ 
           
          // on success fade out the form and show the success message
          $('#loader').hide();
          $('#contact_form input, #contact_form textarea').fadeOut('fast');
          $('.contact_info').fadeOut('fast');
           $('.success').fadeIn();    	
            }
        }); // end ajax

		 return false;  
		} 	
		
	});		
		

		
	// FOOTER TOOLTIPS
	$('.tooltip_link').tipsy({gravity: 's', fade: 'true' });	
		
		
	// FOOTER SOCIAL LIST HOVER
    $(".social_list a").hover(function () {
        $(this).filter(':not(:animated)').animate({
            top: '-=7px'
        });
    }, function () {
        $(this).animate({
            top: '0px'
        });
    });

	



	// SORTABLE PORTFOLIO DROPDOWN SCRIPT
	$('.changer_select li:first-child').addClass('active_li');
	$('.changer_select li:first-child').next('li').addClass('next_child');
	
	// on click on the dropdown list item remove "active" class from the first item and add it to clicked one
	$('.changer_select li a').click(function(){
		$('.changer_select li.active_li').removeClass('active_li'); 
		$(this).closest("li").addClass('active_li').prependTo(".changer_select");
		$('.changer_select li.next_child').removeClass('next_child');
		$('.changer_select li:first-child').next('li').addClass('next_child');
		$('.changer_select li').not('.active_li').hide();		
		$('.list_switch').addClass('show_list');
	return false;
	});
	
	
	$('.list_switch').addClass('show_list');
	
	// on click on dropdown arrow slide down the list for sorting
	$('.list_switch').live('click', function() {
		if ($(this).hasClass('show_list'))
		{
			$(this).removeClass('show_list');
			$('.changer_select li').slideDown('fast');
		}
		else
		{$(this).addClass('show_list');
			$('.changer_select li').not('.active_li').slideUp();
		}		
			return false;
	});
	



	// PORTFOLIO SWITCH BETWEEN THUMBNAILS AND TEXT COULMNS 
	
	//on click on 'List' link fade out thumbnails and show text list
	$('.portfolio_list_switch').click(function() {
		$('a.active_switch').removeClass('active_switch');	
		$(this).addClass('active_switch');
		$('.portfolio_thumbs').fadeOut('fast', function(){		
			$('.portfolio_item_list').fadeIn();		
		});
		return false;	
	});
	
	
	//on lick on 'Thumbnails' link fade out text list and show the thumbnails
	$('.portfolio_thumbs_switch').click(function(){
		$('a.active_switch').removeClass('active_switch');	
		$(this).addClass('active_switch');	
		$('.portfolio_item_list').fadeOut('fast', function(){
			$('.portfolio_thumbs').fadeIn();		
		});
		return false;	
	});
	

}); // DOCUMENT READY FUNCTION ENDS HERE


$(window).load(function() { 
	
	// check if slider exists		
	if ($('#featured_slider').length !== 0)	{
	    $('#featured_slider').nivoSlider();
	
	
		//center slider navigation on the homepage
		if( $('body').hasClass('home'))
		{
			$(".nivo-controlNav").animate({"left": "0px"}, 0);
			$(".nivo-controlNav").animate({"bottom": "10px"}, 0);
			var a_count = $(".nivo-controlNav a").size();
			a_count = parseInt(a_count);
			a_count = a_count * 28;
			var width = a_count;
			var x_pos = ((900 / 2) - (width  / 2)) - 5;
			
			$(".nivo-controlNav").animate({"left": x_pos}, 600);
		}
		
		//center slider navigation on other pages
		else
		{
			$(".nivo-controlNav").animate({"left": "0px"}, 0);
			$(".nivo-controlNav").animate({"bottom": "10px"}, 0);
			//$(".nivo-controlNav").animate({"opacity": "0"}, 0);
			
			var a_count = $(".nivo-controlNav a").size();
			a_count = parseInt(a_count);
			a_count = a_count * 28;
			var width = a_count;
			var x_pos = ((524 / 2) - (width  / 2)) - 5;
			$(".nivo-controlNav").animate({"left": x_pos}, 600);		
		}
	
	}
}); //window load function ends here
