jQuery.noConflict();

jQuery(document).ready(function()
{
var autoslide = jQuery("meta[name=twicet1]").attr('content');
var autoslide_dur = jQuery("meta[name=twicet2]").attr('content');
		
whichlightbox();// activates the lightbox used at the portfolio page
k_menu(); //improves the css dropdown menu with animation and ie6 compatibility
k_fader(".preview_images a",".featured_item"); // this is the mainpages fading script-> when a image is clicked a fadeout followed by a fade in occurs
k_pointer(); // this script manages the pointer below the preview pics
if (autoslide == "1"){
if (autoslide_dur != ""){$duration = autoslide_dur}else{$duration = 7000;}
k_autoslide(".preview_images a",$duration); // this script starts the automatic slideshow, number represents the time between the transition in milliseconds
}
k_newsticker(".ticker span",7000); // starts the newsticker, number defines the time between transitions in milliseconds
k_next_slide(".preview_images a",".featured_item",".show-next"); // shows the next slide
k_form(); // initializes ajax sending of the contact form

twicet_improvements(); // adds some improvements to the site
});

function whichlightbox()
{
	var whichtheme = jQuery("meta[name=twicet4]").attr('content');
	
	if(whichtheme == 1 || whichtheme == 3 )
	{
	jQuery("a[rel^='prettyPhoto'], a[rel^='lightbox[portfolio]']").prettyPhoto({
										theme: 'light_rounded' /* light_rounded / dark_rounded / light_square / dark_square */																		   }); 
	}
	else
	{
	jQuery("a[rel^='prettyPhoto'], a[rel^='lightbox[portfolio]']").prettyPhoto({
										theme: 'dark_rounded' /* light_rounded / dark_rounded / light_square / dark_square */																		   }); 
	}

}


function k_menu()
{
		jQuery("#nav a").removeAttr('title');
		jQuery("#nav ul").css({display: "none"}); // Opera Fix
		
		jQuery(" #nav li").hover(function()
		{
			jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(400,"easeInOutQuart");
		}
		,function()
		{
			jQuery(this).find('ul:first').css({visibility: "hidden"});
		});
		
}


function k_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 k_newsticker($items_to_fade, $display_time)
{
	var $user_display_time = jQuery("meta[name=twicet5]").attr('content');
	
	if($user_display_time != "")
	{
		$display_time = $user_display_time;
	}
	
	tickerchange = setInterval(k_start_ticker, $display_time);
	var $tickeritem = jQuery($items_to_fade);
	var i = 1;
	
	function k_start_ticker()
	{
	$tickeritem.filter(":eq("+i+")").trigger('fade');
	i+1 < $tickeritem.length ? i++ : i = 0;	
	}
	
	  $tickeritem.each(function(i)
	  {
		  jQuery(this).bind("fade",function()
		  {	
			  $new_ticker_item = $tickeritem.filter(":eq("+i+")");
			  
			  if( $new_ticker_item.css("display") == "none" )
			  {
				  $tickeritem.filter(":visible").fadeOut(400, function()
				  {	
					  $new_ticker_item.fadeIn(400);
				  });
			  }
			  
		  });
	  });
}


function k_autoslide($items_to_click, $display_time)
{	
	interval = setInterval(k_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 k_start_autoplay()
	{	
		
		$click_these_items.filter(":eq("+i+")").trigger('click',[true]);
		i+1 < $click_these_items.length ? i++ : i = 0;
		
	}
	
}


function k_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(400, function()
				{	
					$new_item.fadeIn(400);
				});
			}
			
			return false;
			
		});
		
	});
	
}

function k_pointer()
{	
	var $wrapper = jQuery(".preview_images");
	var $slider = jQuery('<div class="pointer"></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},600,"easeOutBack");
		});
		
	});
	
}



function k_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 template_url = jQuery("meta[name=twicet3]").attr('content');
	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: template_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;
	});
}

}


function twicet_improvements()
{
	jQuery(".featured_item").prepend("<div class='corner_left ie6fix'></div><div class='corner_right ie6fix'></div>");
	
	if((jQuery.browser.msie && parseInt(jQuery.browser.version) < 7))
	{
		jQuery("#nav, .navwrap, #main, #featured, #featured_bottom, .content_top, #content, #footer").addClass("ie6fix");
	}
}

/* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/ */
jQuery.easing.jswing=jQuery.easing.swing; jQuery.extend(jQuery.easing,{def:"easeOutQuad",swing:function(e,a,c,b,d){return jQuery.easing[jQuery.easing.def](e,a,c,b,d)},easeInQuad:function(e,a,c,b,d){return b*(a/=d)*a+c},easeOutQuad:function(e,a,c,b,d){return-b*(a/=d)*(a-2)+c},easeInOutQuad:function(e,a,c,b,d){if((a/=d/2)<1)return b/2*a*a+c;return-b/2*(--a*(a-2)-1)+c},easeInCubic:function(e,a,c,b,d){return b*(a/=d)*a*a+c},easeOutCubic:function(e,a,c,b,d){return b*((a=a/d-1)*a*a+1)+c},easeInOutCubic:function(e,a,c,b,d){if((a/=d/2)<1)return b/ 2*a*a*a+c;return b/2*((a-=2)*a*a+2)+c},easeInQuart:function(e,a,c,b,d){return b*(a/=d)*a*a*a+c},easeOutQuart:function(e,a,c,b,d){return-b*((a=a/d-1)*a*a*a-1)+c},easeInOutQuart:function(e,a,c,b,d){if((a/=d/2)<1)return b/2*a*a*a*a+c;return-b/2*((a-=2)*a*a*a-2)+c},easeInQuint:function(e,a,c,b,d){return b*(a/=d)*a*a*a*a+c},easeOutQuint:function(e,a,c,b,d){return b*((a=a/d-1)*a*a*a*a+1)+c},easeInOutQuint:function(e,a,c,b,d){if((a/=d/2)<1)return b/2*a*a*a*a*a+c;return b/2*((a-=2)*a*a*a*a+2)+c},easeInSine:function(e, a,c,b,d){return-b*Math.cos(a/d*(Math.PI/2))+b+c},easeOutSine:function(e,a,c,b,d){return b*Math.sin(a/d*(Math.PI/2))+c},easeInOutSine:function(e,a,c,b,d){return-b/2*(Math.cos(Math.PI*a/d)-1)+c},easeInExpo:function(e,a,c,b,d){return a==0?c:b*Math.pow(2,10*(a/d-1))+c},easeOutExpo:function(e,a,c,b,d){return a==d?c+b:b*(-Math.pow(2,-10*a/d)+1)+c},easeInOutExpo:function(e,a,c,b,d){if(a==0)return c;if(a==d)return c+b;if((a/=d/2)<1)return b/2*Math.pow(2,10*(a-1))+c;return b/2*(-Math.pow(2,-10*--a)+2)+c}, easeInCirc:function(e,a,c,b,d){return-b*(Math.sqrt(1-(a/=d)*a)-1)+c},easeOutCirc:function(e,a,c,b,d){return b*Math.sqrt(1-(a=a/d-1)*a)+c},easeInOutCirc:function(e,a,c,b,d){if((a/=d/2)<1)return-b/2*(Math.sqrt(1-a*a)-1)+c;return b/2*(Math.sqrt(1-(a-=2)*a)+1)+c},easeInElastic:function(e,a,c,b,d){e=1.70158;var f=0,g=b;if(a==0)return c;if((a/=d)==1)return c+b;f||(f=d*0.3);if(g<Math.abs(b)){g=b;e=f/4}else e=f/(2*Math.PI)*Math.asin(b/g);return-(g*Math.pow(2,10*(a-=1))*Math.sin((a*d-e)*2*Math.PI/f))+c},easeOutElastic:function(e, a,c,b,d){e=1.70158;var f=0,g=b;if(a==0)return c;if((a/=d)==1)return c+b;f||(f=d*0.3);if(g<Math.abs(b)){g=b;e=f/4}else e=f/(2*Math.PI)*Math.asin(b/g);return g*Math.pow(2,-10*a)*Math.sin((a*d-e)*2*Math.PI/f)+b+c},easeInOutElastic:function(e,a,c,b,d){e=1.70158;var f=0,g=b;if(a==0)return c;if((a/=d/2)==2)return c+b;f||(f=d*0.3*1.5);if(g<Math.abs(b)){g=b;e=f/4}else e=f/(2*Math.PI)*Math.asin(b/g);if(a<1)return-0.5*g*Math.pow(2,10*(a-=1))*Math.sin((a*d-e)*2*Math.PI/f)+c;return g*Math.pow(2,-10*(a-=1))*Math.sin((a* d-e)*2*Math.PI/f)*0.5+b+c},easeInBack:function(e,a,c,b,d,f){if(f==undefined)f=1.70158;return b*(a/=d)*a*((f+1)*a-f)+c},easeOutBack:function(e,a,c,b,d,f){if(f==undefined)f=1.70158;return b*((a=a/d-1)*a*((f+1)*a+f)+1)+c},easeInOutBack:function(e,a,c,b,d,f){if(f==undefined)f=1.70158;if((a/=d/2)<1)return b/2*a*a*(((f*=1.525)+1)*a-f)+c;return b/2*((a-=2)*a*(((f*=1.525)+1)*a+f)+2)+c},easeInBounce:function(e,a,c,b,d){return b-jQuery.easing.easeOutBounce(e,d-a,0,b,d)+c},easeOutBounce:function(e,a,c,b,d){return(a/= d)<1/2.75?b*7.5625*a*a+c:a<2/2.75?b*(7.5625*(a-=1.5/2.75)*a+0.75)+c:a<2.5/2.75?b*(7.5625*(a-=2.25/2.75)*a+0.9375)+c:b*(7.5625*(a-=2.625/2.75)*a+0.984375)+c},easeInOutBounce:function(e,a,c,b,d){if(a<d/2)return jQuery.easing.easeInBounce(e,a*2,0,b,d)*0.5+c;return jQuery.easing.easeOutBounce(e,a*2-d,0,b,d)*0.5+b*0.5+c}});