/**
 * @author Marcin
 */
/*-----------------------------------------------------------------------
Functions
------------------------------------------------------------------------*/
function form_validation(){
	jQuery(".empty, .contact").each(function(i){									  
				jQuery(this).bind("blur", function(){
				
				var value = jQuery(this).attr("value");
				var check_for = jQuery(this).attr("class");
				var surrounding_element = jQuery(this).parent("p");
				var template_url = jQuery("meta[name=helper]").attr('content');
				
				 jQuery.ajax({
					   type: "POST",
					   url: template_url + "/validate.php",
					   data: "value="+value+"&check_for="+check_for,
					   beforeSend:function(){
						   surrounding_element.attr("class","").addClass("ajax_loading");
						 },
					   error:function(){
						   surrounding_element.attr("class","").addClass("ajax_alert");
						 },
					   success: function(response){
						   if(response == "true"){
							surrounding_element.attr("class","").addClass("ajax_valid");
						   }else{
							surrounding_element.attr("class","").addClass("ajax_false");
						   }
					     }						   
			 });
		 });
	});
}

function validate_all(){
	var my_error;
	jQuery(".ajax_form #send").bind("click", function(){
	my_error = false;
	jQuery(".empty, .contact").each(function(i){
										   
				var value = jQuery(this).attr("value");
				var check_for = jQuery(this).attr("class");
				var surrounding_element = jQuery(this).parent("p");
				var template_url = jQuery("meta[name=helper]").attr('content');
				
				 jQuery.ajax({
					   type: "POST",
					   url: template_url + "/validate.php",
					   data: "value="+value+"&check_for="+check_for,
					   beforeSend:function(){
						   surrounding_element.attr("class","").addClass("ajax_loading");
						 },
					   error:function(){
						   surrounding_element.attr("class","").addClass("ajax_alert");
						 },
					   success: function(response){
						   if(response == "true"){
							surrounding_element.attr("class","").addClass("ajax_valid");
						   }else{
							surrounding_element.attr("class","").addClass("ajax_false");
							my_error = true;
							
						   }
						   if(jQuery(".empty, .contact").length  == i+1){
								if(my_error == false){
									jQuery("#ajax_form").slideUp(400);
									var email	 	= jQuery("#email").attr('value');
									var yourname 	= jQuery("#name").attr('value');
									var contact 	= jQuery("#contacting").attr('value');
									var message 	= jQuery("#message").attr('value');
									var phone	 	= jQuery("#phone").attr('value');
									
									jQuery.ajax({
									   type: "POST",
									   url: template_url + "/send.php",
									   data: "Send=true&email="+email+"&yourname="+yourname+"&contact="+contact+"&message="+message+"&phone="+phone,
									   success: function(response){
									   jQuery("#ajax_response").css({display:"none"}).html(response).slideDown(400);   
										   }
												});																	
									} 
							}
					     }
				 });				 
			});	
	return false;
	});
}

$.fn.slowfade = function(default_options) 
{
	var defaults = 
	{  
		duration:400,
		opacity:1,
		add_inner_html:false, 	//set true for text links
		use_class:false, 		// set to classname
		use_span_class:false, 	// set to classname
		ie6comp:false 			//ie6 compatibility mode, used when ie6 is used and wrapping item has no fixed width, set to classname
	};  
	var default_options = $.extend(defaults, default_options); 
	
	return this.each(function()
	{	
		var my = $(this);
		var $pos = my.css("position") == "absolute" ? "absolute" : "relative";
		var $bg = my.css("background-position") === undefined ? my.css("background-position-x") + ' ' + my.css("background-position-y") : my.css("background-position");
		var $inner_html = default_options.add_inner_html ? my.text() : "";
		
		if(!default_options.use_class)
		{
			my.css({position:$pos, backgroundPosition:"right bottom"});
		}
		else
		{
			my.addClass(default_options.use_class);
		}
		
		if(!default_options.use_span_class)
		{
			$hover_replace = $('<span>'+$inner_html+'</span>').appendTo(my).css(
			{	
				height: my.height(), width: my.width(), backgroundImage: my.css("background-image"), backgroundPosition: $bg,
				cursor: "pointer", position:"absolute",top:"0",left:"0",display:"block"
			});
		}
		else
		{
			$hover_replace = $('<span class="'+default_options.use_span_class+'">'+$inner_html+'</span>').appendTo(my);
		}
		
		if(default_options.ie6comp && $.browser.msie && $.browser.version.substr(0,1)<7)
		{
			$hover_replace.css({height: my.height(), width: my.width()}).addClass(default_options.ie6comp);
		}
					
		
		$hover_replace.hover(
		function()
		{	
			$(this).stop().animate({opacity: 0},default_options.duration);	
			if(default_options.opacity != 1) my.stop().animate({opacity: default_options.opacity},default_options.duration);	
		},
		function()
		{
			$(this).stop().animate({opacity: 1},default_options.duration);
			if(default_options.opacity != 1) my.stop().animate({opacity: 1},default_options.duration);	

		});
	});	
}

$.fn.nospam = function(settings) {
	settings = jQuery.extend({
		replaceText: true, 	// optional, accepts true or false
		filterLevel: 'normal' 	// optional, accepts 'low' or 'normal'
	}, settings);
	
	return this.each(function(){
		e = null;
		if(settings.filterLevel == 'low') { // Can be a switch() if more levels added
			if($(this).is('a[rel]')) {
				e = $(this).attr('rel').replace('/', '@').replace('/', '.');
			} else {
				e = $(this).text().replace('/', '@').replace('/', '.');
			}
		} else { // 'normal'
			if($(this).is('a[rel]')) {
				e = $(this).attr('rel').split('').reverse().join('').replace('/', '@').replace('/', '.');
			} else {
				e = $(this).text().split('').reverse().join('').replace('/', '@').replace('/', '.');
			}
		}
		if(e) {
			if($(this).is('a[rel]')) {
				$(this).attr('href', 'mailto:' + e);
				if(settings.replaceText) {
					$(this).text(e);
				}
			} else {
				$(this).text(e);
			}
		}
	});
};

/*-----------------------------------------------------------------------
Portfolio
------------------------------------------------------------------------*/

$(function() {		   					   
	$('#showcase').cycle({ 
		fx: 'scrollUp',
    	timeout: 0,
		pause: true,
		next: '#pagination a.next',
		prev: '#pagination a.prev',
		height: '338px',
		width: '554px',
		pager: '#pagination .numbers',
    	speed:   1100,
    	startingSlide: 0
    });
	

	//$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex){
		//$('#pagination a.activeSlide').removeClass('activeSlide');
		//$('#pagination .numbers a:eq(' + currSlideIndex + ') a').addClass('activeSlide');
	//};
	
	$('#offer').cycle({ 
		fx: 'scrollLeft',
    	timeout: 10000,
		next: '#pagination a.next',
		prev: '#pagination a.prev',
		width: '905px',
		height: '422px',
		pager: '#test',
    	speed:   1100,
    	startingSlide: 0
    });
	
	$('#pagination a.www').click(function() {$('#offer').cycle(0); $('#offer').cycle('pause'); return false;});	
	$('#pagination a.paper').click(function() {$('#offer').cycle(1); $('#offer').cycle('pause'); return false;});	
	$('#pagination a.banners').click(function() {$('#offer').cycle(2); $('#offer').cycle('pause'); return false;});
	$('#pagination a.callendars').click(function() {$('#offer').cycle(3); $('#offer').cycle('pause'); return false;});
	$('#pagination a.photography').click(function() {$('#offer').cycle(4); $('#offer').cycle('pause'); return false;});
	
});

/*-----------------------------------------------------------------------
Contact
------------------------------------------------------------------------*/
$(function() {
	form_validation();
	validate_all();
	$('a.spamemail').nospam();
});

/*-----------------------------------------------------------------------
Menu
------------------------------------------------------------------------*/
$(function() {
	jQuery("#header>ul>li>a").slowfade({use_span_class:"slowfade",use_class:"slowfade_mod", add_inner_html:true, ie6comp:"ie6fix"});
});
 
 
 







