var Gkids;
var sliderTimer;
var left;
var width;
var kid = 1;
var kidops = 1;
var kids = 0;
var kidsops = 0;
var stop=1;
var stopo=1;

$(document).ready(function(){

	if (jQuery('#showcases #scrollNews').children().size()<4){ 
		$('a.nardown').attr('style','visibility:hidden');
		$('a.narup').attr('style','visibility:hidden');
	};
	
	//get kids for showops
	if($('#showops').is(':visible')){
    	var gethtml = $('#showops #scrollOpinions').html();
    	var getpromo1 = $('#showops #scrollOpinions #ops_1').html();
    	
    	var newhtml = gethtml+'<div style="height:198px;display:block;">'+getpromo1+'</div>';
    	//alert(newhtml);
    	$('#showops #scrollOpinions').html(newhtml);
    }
	
	// kids ?
	// kids = $('#showcases #scrollNews').children().size(); ai un div in plus si atunci foloseste linia urmatoare.
	kids = $('#showcases #scrollNews div').children().size();
	kidsops = $('#showops #scrollOpinions').children().size();

	// date picker
	$('.datePicker').datePicker()
	$('#rezOnSosire').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#rezOnPlecare').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#rezOnPlecare').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#rezOnSosire').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);

	$('a.dp-choose-date').css('margin','2px 3px 0');

	// fancybox
	$("a.view").fancybox({
		centerOnScroll: false
	});
	$("a.galerie").fancybox({
		padding: 0,
		frameWidth: 700,
		frameHeight: 602,
		hideOnContentClick: false,
		centerOnScroll: false
	});
	$("a.galerie_2").fancybox({
		padding: 0,
		frameWidth: 700,
		frameHeight: 500,
		hideOnContentClick: false,
		centerOnScroll: false
	});
	$("a.pagina").fancybox({
		padding: 0,
		frameWidth: 700,
		frameHeight: 700,
		hideOnContentClick: false,
		centerOnScroll: false
	});
	$("a.pagina2").fancybox({
		padding: 0,
		frameWidth: 700,
		frameHeight: 674,
		hideOnContentClick: false,
		centerOnScroll: false
	});
	$("a.stiri").fancybox({
		padding: 0,
		frameWidth: 500,
		frameHeight: 400,
		hideOnContentClick: false,
		centerOnScroll: false
	});
	$("a.localizare").fancybox({
		padding: 0,
		frameWidth: 500,
		frameHeight: 380,
		hideOnContentClick: false,
		centerOnScroll: false
	});

	// efect
	$('#slide div').mouseover(function(){

		var unu = $(this).prev().prev();
		var doi = $(this).prev();
		var trei = $(this).next();
		var patru = $(this).next().next();

		$('#slide h1').css('fontSize','12px');
		$('h1',this).css('fontSize','16px');

		// actual
		$('img',this).animate({
			width: "175px",
			height: "170px"
		},100);
		$(this).animate({
			paddingTop: "0px"
		},100);

		// precedentul
		$('img',doi).animate({
			width: "155px",
			height: "150px"
		},100);
		$(doi).animate({
			paddingTop: "14px"
		},100);

		// urmatorul
		$('img',trei).animate({
			width: "155px",
			height: "150px"
		},100);
		$(trei).animate({
			paddingTop: "14px"
		},100);

		// primul
		$('img',unu).animate({
			width: "135px",
			height: "132px"
		},100);
		$(unu).animate({
			paddingTop: "25px"
		},100);

		// ultimul
		$('img',patru).animate({
			width: "135px",
			height: "132px"
		},100);
		$(patru).animate({
			paddingTop: "25px"
		},100);

	});

	// reset
	var reset = false;
	$('#slide').mouseout(function(){

		if (reset) clearTimeout(reset);

		reset = setTimeout(
			function () {

				$('#slide h1').css('fontSize','12px');
				$('#slide div:eq(2) h1').css('fontSize','16px');

				// actual
				$('#slide div:eq(2) img').animate({
					width: "175px",
					height: "170px"
				},100);
				$('#slide div:eq(2)').animate({
					paddingTop: "0px"
				},100);

				// precedentul
				$('#slide div:eq(1) img').animate({
					width: "155px",
					height: "150px"
				},100);
				$('#slide div:eq(1)').animate({
					paddingTop: "14px"
				},100);

				// urmatorul
				$('#slide div:eq(3) img').animate({
					width: "155px",
					height: "150px"
				},100);
				$('#slide div:eq(3)').animate({
					paddingTop: "14px"
				},100);

				// primul
				$('#slide div:eq(0) img').animate({
					width: "135px",
					height: "132px"
				},100);
				$('#slide div:eq(0)').animate({
					paddingTop: "25px"
				},100);

				// ultimul
				$('#slide div:eq(4) img').animate({
					width: "135px",
					height: "132px"
				},100);
				$('#slide div:eq(4)').animate({
					paddingTop: "25px"
				},100);

			}
		, 1000);

	});

	// scrollTo click
	Gkids = $('#galerieFoto ul').children().size();
	$('#rightArrow').click(function(){ block(2); });

	// numbers only
	$(".nmbOnly").keypress(function(e){
		if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) return false;
	});

});

// scroller
var sc_i;
var sc_h=0;
var sc_t;
var sc_d;

function SCdown(id) {

	sc_d = jQuery('div',id);
	sc_i = jQuery(sc_d).height()-jQuery(id).height();
	sc_i = '-'+sc_i;
	sc_h = sc_h-1;

	if(jQuery(id).height()<=jQuery(sc_d).height()) {

		if(sc_h<sc_i) {
			if (sc_t) clearTimeout(sc_t);
		} else {
			jQuery(sc_d).css("top",sc_h+"px");
			sc_t=setTimeout("SCdown('"+id+"')",1);
		}

	}

}

function SCup(id) {

	sc_d = jQuery('div',id);
	sc_h = jQuery(sc_d).css("top");
	sc_h = sc_h.replace('px','');
	sc_h = parseInt(sc_h)+1;

	if(sc_h>=0) {
		if (sc_t) clearTimeout(sc_t);
	} else {
		jQuery(sc_d).css("top",sc_h+"px");
		sc_t=setTimeout("SCup('"+id+"')",1);
	}

}

function SCstop() {

	if (sc_t) clearTimeout(sc_t);

}

// gallery
function gallery(button) {

	$('#bigImg img').fadeOut('fast',function(){
		//$('#loading').fadeIn('fast',function(){

			vCycleImages = setInterval(function() {
				var bImgLoaded = true;
				var images = new Image();
				images.src = $(button).attr('href');

				if (images.complete == false) bImgLoaded = false;

				if (bImgLoaded) {
					$('#bigImg img').attr('src',$(button).attr('href'));
					//$('#loading').fadeOut('fast',function(){
						$('#bigImg img').fadeIn('fast',function(){
							clearInterval(vCycleImages);
						});
					//});
				}
			}, 1000);

		//});
	});

}

// scroll
var scrollWorking = false;
function block(id) {

	if(!scrollWorking) {
		// check scroll working
		scrollWorking = true;

		// scroll
		$('#galerieFoto').scrollTo('#gallery_'+id, {
			duration:1000,
			axis: 'x',
			onAfter: function(){ scrollWorking = false; }
		});

		// check left
		if(id>1 && $('#leftArrow').is(':hidden')) {
			$('#leftArrow').fadeIn('fast');
		} else if (id==1) {
			$('#leftArrow').fadeOut('fast');
		}

		// check right
		if(id<(Gkids-3) && $('#rightArrow').is(':hidden')) {
			$('#rightArrow').fadeIn('fast');
		} else if (id==(Gkids-3)) {
			$('#rightArrow').fadeOut('fast');
		}

		$('#leftArrow').unbind('click').click(function(){ block(id-1); });
		$('#rightArrow').unbind('click').click(function(){ block(id+1); });

	}

}

// localizare
function localizare() {

	document.write('<div style="display:none" id="localizareSWF"><object width="500" height="380"><param name="movie" value="images/localizare.swf"></param><param name="wmode" value="transparent"></param><embed src="images/localizare.swf" type="application/x-shockwave-flash" wmode="transparent" width="500" height="380"></embed></object></div>');

}

function pclienti(a1,a2,a3,lang) {

	//eror
	var error='';
	var fuller='';
	
	var er1 = a1;
	var er2 = a2;
	var er3 = a3;

	if(lang=='ro') {fuller="Nu ati completat campul "; corect='Nu ati completat corect campul ';}
	if(lang=='en') {fuller="You must fill in "; corect='You did not properly field ';}
	if(lang=='de') {fuller="Du hast nicht Feldes "; corect='Du hast nicht richtig Feld ';}
	if(lang=='it') {fuller="Lei non ha campo "; corect='Hai fatto campo non correttamente ';}
	// start
	// jQuery('.buton2').addClass('loading');

	// info
	var nume = jQuery('#nume').val();
	var email = jQuery('#email_e').val();
	var mesaj = jQuery('#mesaj').val();

	// check
	if(nume.length==0 || nume==' ' || nume==er1) error += fuller+er1+' <br />';
	if(email.length==0 || email==' ' || email==er2) error += fuller+er2+' <br />';
	else{
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(email) == false) error += corect+er2+' <br />';
	}

	if(mesaj.length==0 || mesaj==' ' || mesaj==er3) error += fuller+er3+' <br />';

	if(error.length==0) {

		jQuery('.flerror2').slideUp('slow');

		// post
		jQuery.post('ajax.php?a=pclienti',
			{
				nume: nume,
				oras: email,
				mesaj: mesaj
			},
				function(data)
					{
						// afisare ok
						jQuery('.flerror').fadeIn('fast');
						jQuery('#nume').val(er1);
						jQuery('#email_e').val(er2);
						jQuery('#mesaj').val(er3);
						// stop
						// jQuery('.buton2').removeClass('loading');
					}
		);

	} else {
		 // jQuery('.buton2').removeClass('loading');
		jQuery('.flerror2 p').html(error);
		jQuery('.flerror2').slideDown('slow');
	}

}

function go() {


	// main slider
	$('#showcases #scrollNews').animate({
		top: '-'+(kid*358)+'px'
	}, function(){
		if(kid==kids) {
			$('#showcases #scrollNews').css('top','0px');
			kid = 1;
		}
	});


	kid++;

}

function goops() {


	// main slider
	$('#showops #scrollOpinions').animate({
		top: '-'+(kidops*210)+'px'
	}, function(){
		if(kidops==kidsops) {
			$('#showops #scrollOpinions').css('top','0px');
			kidops = 1;
		}
	});


	kidops++;

}

$(window).load(function () {
	// let`s play
	//slider = setInterval("go();",21000);
	sliderops = setInterval("goops();",21000);
	
	//if(kids<3) clearInterval(slider);
	if(kidsops<3) clearInterval(sliderops);
});


function scroll_news_up() {
	//clearInterval(slider);
	if(stop==1 && kids>2){
		var test = jQuery('#showcases #scrollNews').children().size();
		if (test % 1 == 0) {var test = test + 1;}
		stop=0;

			//alert(test);
			var top=$('#showcases #scrollNews').css('top');

		  $('#showcases #scrollNews').animate({
		    top: ((top=='0px')?'':'+=358')
		  }, 1000, function() {
		    // Animation complete.
		    stop=1;
		  });
	}
}

function scroll_news_down() {
	//clearInterval(slider);
	if(stop==1 && kids>2){
		var test = jQuery('#showcases #scrollNews').children().size();
		//alert(test);

		//if(test%2==0) { var test= test;}
		//if((test1)%2==0) { var test= test+1;}


		test2=((-358)*test)+'px';
		test3=((test-2)*(-358))+'px';
		stop=0;

			var top=$('#showcases #scrollNews').css('top');
		  $('#showcases #scrollNews').animate({
		    top: ((top==test3)?'-=0':'-=358')

		  }, 1000, function() {
		    // Animation complete.
		     stop=1;
		  });
	}
}


function scroll_marci_left(id) {
	if(stop==1){
		var test = jQuery('#showcases #scrollNews').children().size();
		arrow=((-119)*(test-3))+'px';
		if (test % 1 == 0) {var test = test + 1;}
		stop=0;
		
		var top=$('#showcases #scrollNews').css('top');

	  $('#showcases #scrollNews').animate({
	    top: ((top=='0px')?'':'+=119')
	  }, 500, function() {
		  	if(top==arrow) $('a.nardown').attr('style','visibility:visible');
	 		 if(top=='-119px') $('a.narup').attr('style','visibility:hidden');
	  		stop=1;
	  });
	}


}
function scroll_marci_right(id) {
	if(stop==1){
		
		var test = jQuery('#showcases #scrollNews').children().size();
		
		test2=((-119)*(test-3))+'px';
		
		if (test>3)
			arrow=((-119)*(test-4))+'px';
		else
			arrow=((-119)*(test-1))+'px'; 
		stop=0;

		var top=$('#showcases #scrollNews').css('top');
		  $('#showcases #scrollNews').animate({
	    top: ((top==test2)?'-=0':'-=119')
	 	 }, 500, function() {
	 		 if(top==arrow) {
	 			$('a.nardown').attr('style','visibility:hidden');
	 		 }
	 		 if(top=='0px' || top=='auto') $('a.narup').attr('style','visibility:visible');
	 	 	stop=1;
	 	 });
	}

}



function scroll_ops_up() {
	clearInterval(sliderops);
	if(stopo==1 && kidsops>2){
		var test = jQuery('#showops #scrollOpinions').children().size();
		if (test % 1 == 0) {var test = test + 1;}
		stopo=0;

			//alert(test);
			var top=$('#showops #scrollOpinions').css('top');

		  $('#showops #scrollOpinions').animate({
		    top: ((top=='0px')?'':'+=210')
		  }, 1000, function() {
		    // Animation complete.
		    stopo=1;
		  });
	}
}

function scroll_ops_down() {
	clearInterval(sliderops);
	if(stopo==1 && kidsops>2){
		var test = jQuery('#showops #scrollOpinions').children().size();
		//alert(test);

		//if(test%2==0) { var test= test;}
		//if((test1)%2==0) { var test= test+1;}


		test2=((-210)*test)+'px';
		test3=((test-2)*(-210))+'px';
		stopo=0;

			var top=$('#showops #scrollOpinions').css('top');
		  $('#showops #scrollOpinions').animate({
		    top: ((top==test3)?'-=0':'-=210')

		  }, 1000, function() {
		    // Animation complete.
		     stopo=1;
		  });
	}
}

function check_input(id,text){
	var id;
	var text;
	input = $('#'+id).val();
	
	if(input=='' || input==text) $('#'+id).val('');
}
function refresh_input(id,text){
	var id;
	var text;
	input = $('#'+id).val();
	
	if(input=='') $('#'+id).val(text);
}

	

