/**
 * @author Marcin
 */
/*-----------------------------------------------------------------------
Global Vars
------------------------------------------------------------------------*/
var site_url 	= $("meta[name=helper]").attr('content');
var map_status	= $("meta[name=map]").attr('content');
var map_url		= site_url+'/kontakt/siec-dystrybucji/';
var map_area	= $("meta[name=area]").attr('content');

/*-----------------------------------------------------------------------
Functions
------------------------------------------------------------------------*/
function form_validation(){
	$(".empty, .contact").each(function(i){									  
		$(this).bind("blur", function(){
		
			var value = $(this).attr("value");
			var check_for = $(this).attr("class");
			var surrounding_element = $(this).parent("p");
					
			$.ajax({
				type: "POST",
				url: site_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;
	$(".ajax_form #send").bind("click", function(){
		my_error = false;
		$(".empty, .contact").each(function(i){
											   
			var value = $(this).attr("value");
			var check_for = $(this).attr("class");
			var surrounding_element = $(this).parent("p");
			var template_url = $("meta[name=helper]").attr('content');
			
			$.ajax({
				type: "POST",
				url: site_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($(".empty, .contact").length  == i+1){
						if(my_error == false){
							$("#ajax_form").slideUp(400);
							var email	 	= jQuery("#email").attr('value');
							var yourname 	= jQuery("#name").attr('value');
							var contact 	= jQuery("#contacting").attr('value');
							var phone	 	= jQuery("#phone").attr('value');
							var message 	= jQuery("#message").attr('value');
							
							$.ajax({
								type: "POST",
								url: template_url + "/send.php",
								data: "Send=true&email="+email+"&yourname="+yourname+"&contact="+contact+"&message="+message+"&phone="+phone,
								success: function(response){
									$(".sendform").css({display:"none"});
									$("#ajax_response").css({display:"none"}).html(response).slideDown(400);   
								}
							});																	
						} 
					}
		     	}
			});				 
		});	
		return false;
	});
}

$.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);
			}
		}
	});
};

function changeActiveArea (id) {
	( document.getElementById("activeFlash") ) .changeArea ( id );
}

function showActiveArea (id) {
	$('#distributionpl .toggle_container').slideUp();
	$('#distributionpl a[rel^='+id+']').parent().next(".toggle_container").slideToggle("slow");
	changeActiveArea(id);
}

/*-----------------------------------------------------------------------
On Load
------------------------------------------------------------------------*/

$(function() {	 
	form_validation();
	validate_all();
	$('a.spamemail').nospam();
	
	Cufon.replace('#topmenu a', {
		fontFamily: 'cf1',
		hover: true
	});	
	Cufon.replace('.cf2', {
		fontFamily: 'cf2',
		hover: true
	});	
	
	$('#content .page a img').parent().attr('rel', 'prettyPhoto')
	
//	$('#content .page a').filter(function() {
//    	return $(this).attr('href').match(/\.(jpg|png|gif)/);
//	}).attr('rel', 'prettyPhoto');
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		showTitle: false, 
		allowresize: true, 
		theme: 'dark_rounded' 	
	});
	
	$('#topadv .rotator').cycle({
		fx:			'fade',
		height:		'211px'
	});
	
	$('#flashmap').flash(
        { src: site_url+'/swf/mapa_polski.swf',
          id: 'activeFlash',
		  width: 180,
		  wmode: 'transparent',
		  allowScriptAccess: 'sameDomain',
		  flashvars: { site_url: map_url, map_status: map_status, map_area: map_area }
		},
        { version: 9 }
    );
	
	$('#sidebarnews .sidebarnews').click(function(){
		$el = $(this);
		window.location=$(this).find("a.title").attr("href"); return false;
	})
	
	$('#distributionpl .toggle_container').hide();
	$disttrig = $('#distributionpl .trigger');
	$disttrig.css('cursor', 'pointer');
	$disttrig.append(" (kliknij aby zobaczyć adresy dealerów...)");
	$disttrig.click(function(){
		$area 	= $(this).attr('rel')
		$el 	= $(this).parent();
		$el.parent().find('.toggle_container').slideUp();
		$el.next(".toggle_container").slideToggle("slow");
		changeActiveArea($area);
	}); 
	
});
