var $jQ = jQuery.noConflict();

// http://jquery.com/
$jQ(document).ready(function(){
	
	/* START SCROLLABLE GALLERY */
	$jQ(".scrollable").scrollable();
	$jQ(".scrollable_photo").scrollable();
	$jQ(".scrollable_row").scrollable2();
	/* END SCROLLABLE GALLERY */
	
	/* SHOW/HIDE BOX */
	$jQ(".contact_container, .special_offers_container").live('click',function(){
		if ($jQ(this).next().css('display')!='none'){
			$jQ(this).next().hide();
		} else {
			$jQ(this).next().show();
		}
	});
	
	/* END SHOW/HIDE BOX */
	
	/*  VALIDATION  */
	$jQ("#contact_form").validate({
		 rules: {
		  name: {
		   required: true,
		   minlength: 2
		   },
		   email: {
		    required: true,
		    email: true
		   },
		   question: {
		    required: true,
		    minlength: 5
		   }
		  },
		  messages: {
		   name:{
		   required: "Please give Your name",
		   minlength: "Minimal length is 2 signs"
		   /*required: "Prosze podać imię",
		   minlength: "Minimalna długość to 2 znaki"*/
		   },
		   email:{
			required: "Please give Your e-mail",
			email: "E-mail address is invalid"
		    /*required: "Wprowadź adres e-mail",
		    email: "Wprowadź poprawny adres e-mail"*/
		   },
		   question:{
		    required: "Please give Your question",
			minlength: "Minimal lenght is 5 signs"
		   /* required: "Pole hasło nie może być puste",
		    minlength: "Minimalna długość to 5 znaków"*/
		   }
		  },
		  	submitHandler: function(form) {
			  	host = 'http://'+window.location.hostname;
				$jQ(form).ajaxSubmit({
					url: host+"/pl/contact/contact",
					target: ".contact_form"
				})
			}
		});
	
	/*  END VALIDATION  */

	/* FORM TOOLTIPS */
	
	$jQ(".contact_form #name").live('click', function(){
		if ($jQ(this).is(":focus")){
			$jQ('.contact_name_tip').fadeIn();
		}		
	});
	
	$jQ(".contact_form #name").focusout(function(){
		$jQ('.contact_name_tip').fadeOut();		
	});
	
	$jQ(".contact_form #email").live('click', function(){
		if ($jQ(this).is(":focus")){
			$jQ('.contact_email_tip').fadeIn();
		}		
	});
	
	$jQ(".contact_form #email").focusout(function(){
		$jQ('.contact_email_tip').fadeOut();		
	});
	
	$jQ(".contact_form #question").live('click', function(){
		if ($jQ(this).is(":focus")){
			$jQ('.contact_question_tip').fadeIn();
		}		
	});
	
	$jQ(".contact_form #question").focusout(function(){
		$jQ('.contact_question_tip').fadeOut();		
	});

	/* END FORM TOOLTIPS */
	
	/* ADJUSTING SCROLLER */
	
	$jQ('#lcontent_scroll').jScrollPane({
		scrollbarWidth:16,
		scrollbarMargin:10,
		dragMinHeight: 26,
		dragMaxHeight: 27,
		enableKeyboardNavigation: false,
		verticalGutter: 5,
		showArrows: true
	});
	
	/* END ADJUSTING SCROLLER */

	/* SUBMENU SHOW/HIDE */

	$jQ(".subelements_close").live('click', function(){
		if ($jQ(this).closest('.menu_subelements').css('display')!='none'){
			$jQ(this).closest('.menu_subelements').css('display', 'none');
		}		
	});
	
	$jQ('.menu_element_selected .show_hide, .menu_element .show_hide').live('mouseover',function(){
		var submenu = $jQ(this).next().find('.menu_subelements');
		if (submenu && submenu.css('display')=='none'){
			var elements = $jQ('.menu_top_ul_holder .menu_subelements');
			elements.each(function(){
				if ($jQ(this).css('display')!='none'){
					$jQ(this).css('display', 'none');
				}
			});
			submenu.show();
		}
	});
	
	/* END SUBMENU SHOW/HIDE */
	
	
	/* START FANCYBOX */
	$jQ("a.fancybox").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'transitionIn'	:	"elastic",
		'transitionOut'	:	"elastic"	
	});
	/* END FANCYBOX */
	
		
	/* START CHANGE PHOTO */
	$jQ(".changePhoto").click(function(){
		var position = $jQ(this).attr('alt');
		var api = $jQ(".scrollable").data("scrollable");
		if (!api){
			api = $jQ(".scrollable_photo").data("scrollable");
		}
	
		api.seekTo(position, 0);
		var position = $jQ('#changePosition img').offset();
		$jQ('html, body').animate({
			scrollTop: position.top+'px'
			}, 300);
	});
	/* END CHANGE PHOTO */
		
		
});


$jQ(window).load(function () {

	
	/* START NIVO SLIDER AND DOWNLOADING PHOTOS IN BACKGROUND */
	
	var total = $jQ("#slider img").length;
	
	$jQ('#slider').nivoSlider({
		startSlide: 0,
        effect: 'fade',
        pauseTime: 5000,
        slices: 1,
        directionNav: false, //Next & Prev
		afterLoad: function(){
	    	$jQ('.nivo-controlNav a').each(function() {
	    		$jQ(this).hide();
	    	});
	    	var nivo_control = $jQ('.nivo-controlNav');
	    	nivo_control.css("height", "22px");
	    	nivo_control.css("min-width", "20px");
	    	nivo_control.css("background-image", "url('http://www.whitesandvillas.com/images/slider/ajax-loader.gif')");
	    	nivo_control.css("background-position", "center center");
	    	nivo_control.css("background-repeat", "no-repeat");
	    	getImages(1);
		}
	});
	
	var slider_obj = $jQ('#slider').data('nivoslider');
	if (slider_obj){
		slider_obj.stop();
	}	
	
	function getImages(photo){
		if (photo==total){
	    	$jQ('.nivo-controlNav a').each(function() {
	    		$jQ(this).fadeIn();
	    	});
	    	$jQ('.nivo-controlNav').css("background-image", "")
			slider_obj.start();
		} else {
			var img = new Image();
			$jQ.ajax({
				url: 'http://www.whitesandvillas.com/index.php/pl/gallery/images',
				data: { id : photo },
				type: "POST",
				cache: true,
				success: function(data) {
					img.src = data;
				}
			});
			img.onload = function() {
				$jQ('#nivo_photo_'+photo).attr('src', img.src);
				getImages(photo+1);
			}
		}
	}

	/* END NIVO SLIDER AND DOWNLOADING PHOTOS IN BACKGROUND */
	
		
});
