var $j = jQuery.noConflict();

$j(document).ready(function(){
	
	
	//cufon
	Cufon.replace('h1, h2, .day, .month, .login-box h3');
	
	//slidedown login
	$j('.login-link').toggle(
	function()
	{
		$j('.login-box').animate({'height':'toggle'}, 200);
	},
	function()
	{
		$j('.login-box').animate({'height':'toggle'}, 200);
		$j('.login-error').html('');
	});
	//submit login form
	$j('.loginBtn').click(function(){
		$j('#login-form').submit();
	});
	//login submission
	$j('#login-form').submit(function(){
		
		$j('.login-error').html("Sending request...");
		
		//send data to processing script
		$j.ajax({
		
			url: 'wp-content/themes/Clearmedia/includes/process-login.php',
			type: 'POST',
			data: {
			
				user:$j('#user').val(),
				pass:$j('#pass').val()
			
			},
			success: function(data){
				
				$j('.login-error').html(data);
				
			},
			error: function(){
				
				alert('There seems to have been an error sorry');
			}
		
		});
		
		//prevent submitting
		return false;
	});
	
	//slidedown company
	$j('.company-link').toggle(
	
	function(){
		$j('.contact').css({'display':'none'});
		$j(this).addClass('on');
		$j('.company').animate({'height':'toggle'}, 200);
	
	}, function(){
	
		$j('.contact').css({'display':'none'});
		$j(this).removeClass('on');
		$j('.company').animate({'height':'toggle'}, 200);
		
	});
	$j('.closeBtn.com').click(function(){
		$j('.company-link').removeClass('on');
		$j('.company').animate({'height':'toggle'}, 200);
	});
	
	//slidedown contact
	$j('.contact-link').toggle(
	
	function(){
	
		$j('.company').css({'display':'none'});
		$j(this).addClass('on');
		$j('.contact').animate({'height':'toggle'}, 200);
	
	}, function(){
	
		$j('.company').css({'display':'none'});
		$j(this).removeClass('on');
		$j('.contact').animate({'height':'toggle'}, 200);
		$j('#contact-form').css({'display':'inline'});
		$j('.form-error').html('');
	});
	$j('.closeBtn.con').click(function(){
		$j('.contact-link').removeClass('on');
		$j('.contact').animate({'height':'toggle'}, 200);
		$j('#contact-form').css({'display':'inline'});
		$j('.form-error').html('');
	});
	
	//animate page to anchor
	$j('.next-article').click(function(){
		$j('html,body').animate({scrollTop: $j("#"+$j(this).attr('rel')).offset().top},'slow');
	});
	
	
	
	//toggle client logos / descriptions
	$j('.client-logo').mouseenter(
	function(){
		$j(this).fadeOut(200);
		$j(this).parent().find('.client-data').fadeIn(200);
	});
	 
	$j('.client-data').mouseleave(function(){
		$j(this).fadeOut(200);
		$j(this).parent().find('.client-logo').fadeIn(200);
	});
	
	//default values for form
	formArr = {
		"name":"name",
		"email":"email",
		"phone":"phone",
		"enquiry":"enquiry",
		"code":"please enter verification code"
	};
	$j.each(formArr, function(key, value){
			
			//remove default value on focus
			$j("#"+key+"").focus(function(){
			
				if($j(this).val() == value)
				{
					$j(this).val('');
				}
			
			});//remove defaults
			
			//return default values if nothing entered in field
			$j("#"+key+"").blur(function(){
				
				if($j(this).val() == '')
				{
					$j(this).val(value);
				}
				
			});//return defaults
			
	});
	
	//validate
	function validate()
	{
		//array to hold fields not right
		var fields = new Array();
		//check for default values and empty if so
		$j.each(formArr, function(key, value){
		
			if($j("#"+key).val() == value)
			{
				fields.push(key);
			}
		});
		if(fields.length > 0)
		{
			var d = fields.join(',');
			alert("The following fields are required: "+d);
			return false;
		}
		else
		{
			return true;
		}
	}
	$j('.send').click(function(){
	
		if(validate())
		{
			$j('#contact-form').submit();
		}
	
	});
	//process form
	$j('#contact-form').submit(function(){
		
		
		$j('.form-error').html('Sending request, please wait');
		
		//send data to processing script
		$j.ajax({
		
			url: 'wp-content/themes/Clearmedia/includes/process-form.php',
			type: 'POST',
			data: {
			
				name:$j('#name').val(),
				email:$j('#email').val(),
				phone:$j('#phone').val(),
				enquiry:$j('#enquiry').val(),
				code:$j('#code').val()
			
			},
			success: function(data){
				
				$j('.form-error').html(data);
				if(data == 'Your email has been sent, thankyou')
				{
					$j('#contact-form').css({'display':'none'});
				}
				
			},
			error: function(){
				
				alert('There seems to have been an error sorry');
			}
		
		});
		
		//prevent submitting
		return false;
	});
	
	
	
	//slideshow stuff
	//for each post
	$j('.slide-show').each(function(){
		
		//number of images
		var numberOfImages = $j(this).find('.show ul li').length;
		var currentImage = 0;
		
		if(numberOfImages == 1)
		{
			$j(this).find('.slide-show-bar > .right-arrow').css({'display':'none'});
			$j(this).find('.slide-show-bar > .left-arrow').css({'display':'none'});
		}
		
		$j(this).find(".total").html(numberOfImages);
		$j(this).find(".num").html(currentImage + 1);
		
		//fade in initial image
		$j(this).find('.show ul li').eq(currentImage).fadeIn(1000);
		
		//on click functions
		$j(this).find('.slide-show-bar > .right-arrow').click(function(){
			
			$j(this).parent().parent().find('.show ul li').eq(currentImage).fadeOut(300);
			if(currentImage == numberOfImages - 1)
			{
				currentImage = 0;
			}
			else
			{
				currentImage++;
			}
			$j(this).parent().parent().find('.show ul li').eq(currentImage).fadeIn(300);
			$j(this).parent().find(".num").html(currentImage + 1);
		});
		$j(this).find('.slide-show-bar > .left-arrow').click(function(){
			
			$j(this).parent().parent().find('.show ul li').eq(currentImage).fadeOut(300);
			if(currentImage == 0)
			{
				currentImage = numberOfImages - 1;
			}
			else
			{
				currentImage--;
			}
			$j(this).parent().parent().find('.show ul li').eq(currentImage).fadeIn(300);
			$j(this).parent().find(".num").html(currentImage + 1);
		});
		
	});//slideshow
	
	
	//animate projects that are visible
	$j('.web').masonry({
	
		 singleMode:false,
  		 animate: true,
		 itemSelector: '.item:not(.invis)'
	});
	
	//on window resize
	$j(window).resize(function(){
		$j('.web').masonry();
	});
	
	//only show projects that are visible
	$j('.filter').change(function(){
	
		var c = $j(this).val();
		if(c == 'all')
		{
			$j('.web').children('.invis').toggleClass('invis').fadeIn(200);
		}
		else
		{
			// hide invisible boxes
			$j('.web').children().not('.'+c).not('.invis').toggleClass('invis').fadeOut(200);
			// show hidden boxes
			$j('.web').children('.'+c+'.invis').toggleClass('invis').fadeIn(200);
		}
		$j('.web').masonry();
		return false;
	});
	

});//doc on ready























