
// slideshow settings
var fadeDuration=1000;
var slideDuration=4000;
var currentIndex=1;
var nextIndex=1;
var num_slides = 0;
var timer = null;
var timer2 = null;

$(function()
{
	// slideshow
	setup_slideshow();
	
	// contact form stuff
	$("#contact_btn").click(function(){
		if($("#contact_form").is(":visible"))
		{
			$("#contact_form fieldset").fadeOut(function(){
				$("#contact_form ").slideUp();					
			});
		} else {
			$("#contact_form").slideDown(function(){
				$("#contact_form fieldset").fadeIn("slow");					
			});
		}
		return false;
	});
	
	$('.contact_text, .contact_textarea').each(function() {
		var default_value = this.value;
		$(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
			}
		});
	});
	
	$('#cmd_cancel').click(function(){
		$("#contact_form fieldset").fadeOut(function(){
			$("#contact_form ").slideUp();
			$('#cmd_cancel').html('Cancel');
			$("#cmd_send").css("background-image", "url(/files/images/structure/icon.jpg)").html("Send").fadeIn();
			$('#contact_message').removeClass("error").html('').css("display", "none");
			$('#txt_name').val('Name');
			$('#txt_email').val('Email');
			$('#txt_number').val('Telephone');
			$('#txt_message').val('Message');
		});
		return false;
	});
	
	$("#cmd_send").click(function(){
		send_btn = $(this)
		send_btn.css("background-image", "url(/files/images/structure/icon_spin.gif)").html("Sending...");	
		$('#contact_message').fadeOut();
		$.post("/files/ajax/contact.php", { 
			txt_name:$('#txt_name').val(),
			txt_email:$('#txt_email').val(),
			txt_number:$('#txt_number').val(),
			txt_message:$('#txt_message').val()
		}, function(data){
			if(data.status === 'error')
			{
				$('#contact_message').addClass("error");
				send_btn.css("background-image", "url(/files/images/structure/icon.jpg)").html("Send");	
			} else {
				$('#contact_message').removeClass("error");
				send_btn.fadeOut();
				$('#cmd_cancel').html('Close');
			}
			$('#contact_message').html(data.message).fadeIn();
		}, "json");				 
		return false;						 
	});
	
	
	
	// portfolio
	$(".show_portfolio").click(function(){
		ref = $(this).attr("ref");	
		
		$('.portfolio').slideUp();	
		$(".show_portfolio").find("span").css("background-image","url(/files/images/structure/extend.png)");	
		
		$('.portfolio[ref="'+ref+'"]').slideDown();	
		$('.show_portfolio[ref="'+ref+'"]').find("span").css("background-image","url(/files/images/structure/collapse.png)");
		return false;								
	});
	
	$('.show_portfolio[ref="'+cs_cat+'"]').find("span").css("background-image","url(/files/images/structure/collapse.png)");
	
	// clients boxes
	$(".client_item").hover(
	  function () {
		$(this).find(".client_info").fadeIn();
	  }, 
	  function () {
		$(this).find(".client_info").fadeOut();
	  }
	);
	
	// main nav effect
	$("#main_nav").lavaLamp({ });
	
	// video playlist
	$("ul.videos_list").ytplaylist();
	
});




function setup_slideshow()
{
	/* redshift slide show */
	num_slides = $('ul.slideshow li').length;
	
	if (num_slides > 1)
	{
		for (i=1;i<=$('ul.slideshow li').length;i++)
		{			
			$('#slides_pager').append('<li><a href="#" class="goto_slide" ref="'+i+'"></a></li> ');
		}
	}
	$("ul.slideshow li").css({opacity: 0.0});
	$("ul.slideshow li:nth-child("+nextIndex+")").addClass("show").animate({opacity: 1.0}, fadeDuration);
	$("#slides_pager li:nth-child("+currentIndex+")").addClass("activeSlide")
	$(".slide_title").html( $("ul.slideshow li:nth-child("+currentIndex+")").find("img").attr("title") );
	$(".slide_description").html( $("ul.slideshow li:nth-child("+currentIndex+")").find("img").attr("alt") );
	
	
	$(".goto_slide").click(function(){gotoSlide($(this).attr("ref"));	});
	$(".next_slide").click(function(){nextSlide();});
	$(".prev_slide").click(function(){prevSlide();});
	
	$('#gallery').hover(function() { 
			$(".prev_slide").stop().animate({ left: -31, width: 30 }, 300);
			$(".next_slide").stop().animate({ right: -31, width: 30 }, 300);
			clearInterval(timer);
		}, function() {
			$(".next_slide").stop().animate({ right: -19, width: 18 }, 300);
			$(".prev_slide").stop().animate({ left: -19, width: 18 }, 300);
			if (num_slides > 1)
			{
				timer = setInterval('nextSlide()',slideDuration);
			}
	});
	
	
	if (num_slides > 1)
	{
		timer = setInterval("nextSlide()",slideDuration);
	}
	
	
	// clients carousel
	var auto_slide_seconds = 5000;
	$('#carousel_ul li:first').before($('#carousel_ul li:last'));
	var timer2 = setInterval('slide("right")', auto_slide_seconds);
	$('#hidden_auto_slide_seconds').val(auto_slide_seconds);
	$('#carousel_ul').hover(function(){
		clearInterval(timer2)
	},function(){
		timer2 = setInterval('slide("right")', auto_slide_seconds);
	});
	
	$(".left_scroll").click(function(){	slide("left");	 return false;		 });
	$(".right_scroll").click(function(){slide("right");	 return false;		 });
	$('#carousel_container').hover(function() { 
			$('.left_scroll').stop().animate({ left: -31, width: 30 }, 300);
			$('.right_scroll').stop().animate({ right: -31, width: 30, "background-position": 0 }, 300);
		}, function() {
			$('.right_scroll').stop().animate({ right: -19, width: 18, "background-position": "-13px" }, 300);
			$('.left_scroll').stop().animate({ left: -19, width: 18 }, 300);
	});
	
	
	
	
}

function prevSlide(){
	nextIndex =(parseInt(currentIndex)- 1);
	if(nextIndex < 1)
	{
		nextIndex =$('ul.slideshow li').length;
	}
	change_slide();
	currentIndex = nextIndex;
}


function nextSlide(){
	nextIndex =(parseInt(currentIndex)+ 1);
	if(nextIndex > $('ul.slideshow li').length)
	{
		nextIndex =1;
	}
	change_slide();
}	

function change_slide()
{
	$("'ul.slideshow li:nth-child("+nextIndex+")'").addClass('show').stop().animate({opacity: 1.0}, fadeDuration );	
	$("ul.slideshow li:nth-child("+currentIndex+")").animate({opacity: 0.0}, fadeDuration).removeClass('show');
	currentIndex = nextIndex;
	$("#slides_pager li").removeClass('activeSlide')	
	$("#slides_pager li:nth-child("+currentIndex+")").addClass('activeSlide')	
	$(".slide_title").html( $("ul.slideshow li:nth-child("+currentIndex+")").find("img").attr("title") );
	$(".slide_description").html( $("ul.slideshow li:nth-child("+currentIndex+")").find("img").attr("alt") );
}


function gotoSlide(id)
{
	nextIndex = id;
	change_slide()
	currentIndex = id;
	if(nextIndex > $('ul.slideshow li').length)
	{
		nextIndex = 1;
	} else {
		nextIndex = (parseInt(currentIndex)+ 1);
	}
}	

// clients carousel movement
function slide(where){
	var item_width = $('#carousel_ul li').outerWidth() + 20 ;
	if(where == 'left'){
		var left_indent = parseInt($('#carousel_ul').css('left')) + item_width;
	}else{
		var left_indent = parseInt($('#carousel_ul').css('left')) - item_width;
	}
	
	$('#carousel_ul:not(:animated)').animate({'left' : left_indent},1000,function(){
		if(where == 'left'){
			$('#carousel_ul li:first').before($('#carousel_ul li:last'));
		}else{
			$('#carousel_ul li:last').after($('#carousel_ul li:first'));
		}
		$('#carousel_ul').css({'left' : '-220px'});
	});
}

