var $j = jQuery.noConflict();

$j(document).ready(function(){



if($j('.other').length){

    $j('.other nav').jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
		speed: 1800,
		auto: true
    });

}



if($j('[data-href],[cite]').length){
	
	$j('[data-href],[cite]').click(function() {

		
		var location = '';
		
		if($j(this).data('href')){
			location = $j(this).data('href');
		}

		if($j(this).attr('cite')){
			location = $j(this).attr('cite');
		}

		document.location = location;
		
	});
	
}



if($j('merquee').length){
	
$j('merquee').marquee();
	
}


if($j('.zoom').length){
	
	$j('.zoom').fancybox();	
	
}

if($j('form#wiadomosc').length){
	obslugaPomiarForm.uruchom();
}


if($j('.wydarzenia table').length){

	$j('.wydarzenia table.close tfoot').click(function(){
		
			$j(this).parent().toggleClass('close');
			$j(this).remove();
	});

	
}



if($j('#slideshow').length){





$j.each($j('img[data-src]'), function(k, v){
	
	var img = new Image(); 
	img.src = $j(this).attr('data-src'); 
	img.title = $j(this).attr('data-item');

	img.onload = function(){
	 	$j('img[data-item="'+this.title+'"]').attr('src',this.src);
	}


 });

	
	$j('#slideshow ul.slide').cycle({ 
		fx:    'fade', 
		speed:  500,
		delay: 8000,
		timeout: 8000,
	    pager:  '#pager'
		
	 });	
	
}

});


/*
    var stack = []; 
 
    // preload images into an array; we will preload beach3.jpg - beach8.jpg 
    for (var i = 3; i < 9; i++) { 
        var img = new Image(200,200); 
        img.src = 'images/beach' + i + '.jpg'; 
        $(img).bind('load', function() { 
            stack.push(this); 
        }); 
    }  
 
    // start slideshow 
    $('#slideshow').cycle({ 
        timeout:  2000, 
        before:   onBefore 
    }); 
 
    // add images to slideshow 
    function onBefore(curr, next, opts) { 
        if (opts.addSlide) // <-- important! 
            while(stack.length) 
                opts.addSlide(stack.pop());  
    }; 

*/


obslugaPomiarForm = function(){

	config = {
		mailum : "&"+$j("meta[name=mailum]").attr("content")+"=1",
//		recipcient : "con_id="+$("meta[name=recipcient]").attr("content")+"&",
		postcard:  $j("form#wiadomosc")
		
	}

	function uruchom(){
		

// $j('form#wiadomosc input[title!=""], form#wiadomosc textarea').hint();

		config.postcard.validate({
			debug:true,														 
			rules:{
				"name":{
					required: true
				},
				"email":{
					required: true,
					email:true
				},
				"tekst":{
					required: true
				}
				
			},
			messages: {
				contact_name: "Wpisz imie, nazwisko lub nazwe.",
				contact_text: "Wpisz wiadomosc",
				contact_email: {
					required: "Wpisz adres E-Mail.",
					email: "Wpisz prawidlowy adres E-Mail"
					}, 
			},


		showErrors: function(errorMap, errorList) {
      if(this.numberOfInvalids()){
				
//				config.postcard.effect("shake",{times:1,distance:5},50);


			}
			this.defaultShowErrors();
		},
			submitHandler: function(form) {
				console.info(location);
				text = encodeURIComponent("Wiadomosc :\r\n\r\nNazwa:\t"+$j('#contact_name').val()+"\r\nE-Mail:\t"+$j('#contact_email').val()+"\r\nWiadomosc:\t"+$j('#contact_text').val());
				$j.ajax({
					type: "POST",
					url: "index.php?option=com_contact",
					data: "con_id=1&subject=wiadomosc&"+config.postcard.serialize()+""+config.mailum+"&text="+text,
					success: function(html) {
						config.postcard.addClass("done");
						$j("form#wiadomosc").html("<div class=\"ok\">Wiadomosc zostala wyslana, dziekujemy.</div>");
					},
					complete: function(complet){
						if(complet.status == 200){
							
							$j("form#wiadomosc").html("<div class=\"ok\">Wiadomo\u015b\u0107 zosta\u0142a wys\u0142ana, dzi\u0119kujemy.</div>");
							
//							_gaq.push(['_trackEvent', 'Wiadomosci', 'Wiadomosc wyslana', 'Wiadomosc wyslana ze strony '+$('link[rel=canonical]').attr('href')+' do kontaktu recipcient '+$("meta[name=recipcient]").attr("content")]);
							
						}else{
							$j("form#wiadomosc").html("<div class=\"wrong\">B\u0142\u0105d wys\u0142ania wiadomo\u015bci, spr\u00f3buj wys\u0142a\u0107 wiadomo\u015b\u0107 pod adres <a href=\"mailto:biuro@matuszewski.com.pl\">biuro@matuszewski.com.pl</a></div>");
							
//							_gaq.push(['_trackEvent', 'Wiadomosci', 'B\u0142\u0105d wys\u0142ania', 'Wiadomosc nie wyslana ze strony '+$('link[rel=canonical]').attr('href')+' do kontaktu recipcient '+$("meta[name=recipcient]").attr("content")]);							
						}
						

						
					}

				 });
					return false;
				}
		});
}

	return {
		config:config,
		uruchom:uruchom
	}
	
}();

