/* jQuery Initiate */
$(document).ready(function() {

	// Menu
	ddsmoothmenu.init({
		mainmenuid: "smoothmenu1",
		orientation: 'h',
		classname: 'ddsmoothmenu',
		contentsource: "markup"
	})

	// Project Page Slider
	kanikar_fader(".preview_images a",".featured_item");
	kanikar_pointer();
	kanikar_autoslide(".preview_images a",5000);
	kanikar_next_slide(".preview_images a",".featured_item",".show-next");
	kanikar_form();
	
	// Thumbnail
	$("ul.gallery li").hover(function() {
		
		var thumbOver = $(this).find("img").attr("src");
		
		$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});

		$(this).find("span").stop().animate({opacity: 0}, 1000);
	} , function() { 

		$(this).find("span").stop().animate({opacity: 1}, 1000);
	});
	
	// Home Page Slider
	$("#slider").homeslider({
		timeOut: 5000      
	});
	
});

function kanikar_next_slide($items_to_click, $items_to_fade,$next)
{
	$items = jQuery($items_to_fade);
	$click_these_items = jQuery($items_to_click);
	var x = 0;
	
	$items.each(function(i)
	{
		jQuery(this).find($next).click(function()
		{
		i+1 < $click_these_items.length ? x = i + 1 : x = 0;	
		$click_these_items.filter(":eq("+x+")").trigger('click');
		return false;
		});
	});
}

function kanikar_autoslide($items_to_click, $display_time)
{	
	interval = setInterval(kanikar_start_autoplay, $display_time);
	var $click_these_items = jQuery($items_to_click);
	var i = 1;
	
	$click_these_items.click(function($eventobject, $autoplay)
	{
		if(interval && !$autoplay) clearInterval(interval);
	});
	
	
	function kanikar_start_autoplay()
	{	
		
		$click_these_items.filter(":eq("+i+")").trigger('click',[true]);
		i+1 < $click_these_items.length ? i++ : i = 0;
		
	}
	
}


function kanikar_fader($items_to_click, $items_to_fade)
{	
	var $item = jQuery($items_to_fade);
	
	jQuery($items_to_click).each(function(i)
	{
		jQuery(this).click(function()
		{	
			$new_item = $item.filter(":eq("+i+")");
			
			if( $new_item.css("display") == "none" )
			{
				$item.filter(":visible").fadeOut(1000, function()
				{	
					$new_item.fadeIn(1000);
				});
			}
			
			return false;
			
		});
		
	});
	
}

function kanikar_pointer()
{	
	var $wrapper = jQuery(".preview_images");
	var $slider = jQuery('<div></div>').appendTo($wrapper);
	
	var $slider_half = $slider.width()/2;
	jQuery(".preview_images a").each(function(i)
	{
		jQuery(this).click(function()
		{	
			jQuery(".current_prev").removeClass('current_prev');
			jQuery(this).addClass('current_prev');
			
			$image_pos = jQuery(this).position();
			$newposition = $image_pos.left + jQuery(this).width()/2 - $slider_half;
			$slider.animate({"left":$newposition},1000,"easeOutBack");
		});
		
	});
	
}

function kanikar_form(){
	if(!(jQuery.browser.msie && parseInt(jQuery.browser.version) < 7)) // ajax contact form disabled in ie6, it does work but ie6 shifts layout on error :P
	{
		
	var my_error;
	jQuery(".ajax_form #send").bind("click", function(){
											 
	my_error = false;
	jQuery(".ajax_form #name, .ajax_form #message, .ajax_form #email ").each(function(i){
				
				

				
				
				var value = jQuery(this).attr("value");
				var check_for = jQuery(this).attr("id");
				var surrounding_element = jQuery(this).parent();
				if(check_for == "email"){
					if(!value.match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/)){
						
						surrounding_element.attr("class","").addClass("error");
						
						my_error = true;
						}else{
						surrounding_element.attr("class","").addClass("valid");	
						}
					}
				
				if(check_for == "name" || check_for == "message"){
					if(value == ""){
						
						surrounding_element.attr("class","").addClass("error");
						
						my_error = true;
						}else{
						surrounding_element.attr("class","").addClass("valid");	
						}
					}
						   if(jQuery(".ajax_form #name, .ajax_form #message, .ajax_form #email").length  == i+1){
								if(my_error == false){
									jQuery(".ajax_form").slideUp(400);
									
									var $datastring = "ajax=true";
									jQuery(".ajax_form input, .ajax_form textarea").each(function(i)
									{
										var $name = jQuery(this).attr('name');	
										var $value = jQuery(this).attr('value');
										$datastring = $datastring + "&" + $name + "=" + $value;
									});
																		
									
									jQuery(".ajax_form #send").fadeOut(100);	
									
									jQuery.ajax({
									   type: "POST",
									   url: "send.php",
									   data: $datastring,
									   success: function(response){
									   jQuery(".ajax_form").before("<div class='ajaxresponse' style='display: none;'></div>");
									   jQuery(".ajaxresponse").html(response).slideDown(400); 
									   jQuery(".ajax_form #send").fadeIn(400);
									   jQuery(".ajax_form #name, .ajax_form #message, .ajax_form #email , .ajax_form #website").val("");
										   }
										});
									} 
							}
					});
			return false;
	});
}

}