//Domready
var global;
var slide = 0;
var t, o, tmp;

var sliderCount = 0;
var currentSlide;
var clickedSlide;
var expandedWidth;
var slideSpeed = 500;
var galleryImage;
var dropDownOn = false;
var shareTimer = 0;


$(document).ready(function(){
	if ($('h1').length>0){
		$('.h1').html($('h1').html()).show();
	}

	$(".search-input").bind("focus", function(){
		if ($(this).attr('title')=="") $(this).attr('title',$(this).val());
		$(this).val("");
	});
	$(".search-input").bind("blur", function(){
		if ($(this).val()=="") $(this).val($(this).attr('title'));
	});


 //Caption Sliding (Partially Hidden to Visible)  
     $('.boxgrid.caption').hover(function(){  
         $(".cover", this).stop().animate({top:'88px'},{queue:false,duration:160});
     }, function() {  
         $(".cover", this).stop().animate({top:'124px'},{queue:false,duration:160});
     });
	set_input_cifre();

	$("ul.menu li").not('.selected,.retur,.filtre').bind("mouseenter mouseleave", function (e) {
      $(this).toggleClass("over");
    });

	$('#articol p:first').css("margin-top","0");
	$("ul.articole li, ul.magazine li").not('.selected').bind("mouseenter mouseleave", function (e) {
      $(this).toggleClass("over");
    });
	$('#articol img[align="left"]').css("margin-right",'10px');

	$('#banner').each(function()
		{
			$(this).data('currentImage',$(this).children('.banner-image:first-child'));
		});

		setTimeout('nextBannerImage()',5000);

	// image gallery browser

	// set first image
	if($('.gallery-image').length)
	{
		galleryImage = $('.gallery-image').first();
		$('#gallery-nav a').each(function()
		{
			$(this).data('currentImage',false);
			$(this).hover(function()
			{
				if(!$(this).data('currentImage')) $(this).children('img').attr('src',($(this).children('img').attr('src').replace('off','on')));
				$(this).addClass('over');
			},function()
			{
				if(!$(this).data('currentImage')) $(this).children('img').attr('src',($(this).children('img').attr('src').replace('on','off')));
				$(this).removeClass('over');
			});
			$(this).click(function()
			{
				clearTimeout(gInterval);
				var t = $(this).attr('title');
				if(t == 'Next')
				{
					var g = galleryImage.next('.gallery-image');
					if(!g.length) g = $('.gallery-image').first();
					galleryImage.fadeOut();
					g.fadeIn();
					galleryImage = g;
				} else if(t == 'Prev')
				{
					var g = galleryImage.prev('.gallery-image');
					if(!g.length) g = $('.gallery-image').last();
					galleryImage.fadeOut();
					g.fadeIn();
					galleryImage = g;
				} else
				{
					var imageNum = parseInt(t);
					var g = $('#gallery-'+imageNum);
					if(g.length && g.attr('id') != galleryImage.attr('id'))
					{
						//alert('loading image ' + imageNum);
						galleryImage.fadeOut();
						g.fadeIn();
						galleryImage = g;
					}
				}
				setCurrentImage();
				autoGallery();
			});
		});

		setCurrentImage();
		autoGallery();
		hideGalleryBrowser();

		$('#banner').mouseenter(function()
		{
			showGalleryBrowser();
		}).mouseleave(function()
		{
			hideGalleryBrowser();
		});
	}

	$('a.lightbox').lightBox({
	imageBtnClose: 'images/fermer.gif',
	imageBtnPrev: 'images/gallery-nav-bg.png',
	imageBtnNext: 'images/gallery-nav-bg.png',
	txtImage: 'Carte',
	txtOf: 'de'
   }); // Select all links with lightbox class

})

var autoGallery = function()
{
	gInterval = setInterval(function()
		{
			$('#gallery-next-prev a').last().click();
		},5000);
}

var hideGalleryBrowser = function()
{
	if ($('.nohide').length>0) return false;
	galleryNavTimer = setTimeout(function()
	{
		$('#gallery-nav').hide('slide',{direction:'down'},300);
	},2000);
}

var showGalleryBrowser = function()
{
	clearTimeout(galleryNavTimer);
	if($('#gallery-nav').is(':hidden')) $('#gallery-nav').show('slide',{direction:'down'},300);
}

var setCurrentImage = function()
{
	// set link up as selected

	var n = galleryImage.attr('id').replace('gallery-','');
	$('#gallery-nav a').each(function()
	{
		if(n != $(this).attr('title')) {
			if($(this).data('currentImage')) $(this).children('img').attr('src',($(this).children('img').attr('src').replace('on','off')));
			$(this).data('currentImage',false);
			$(this).removeClass('selected');
		}
		else {
			$(this).data('currentImage',true);
			$(this).children('img').attr('src',($(this).children('img').attr('src').replace('off','on')));
			$(this).addClass('selected');
		}
	});
	//alert(n);
}

nextBannerImage = function()
{
	//alert('showing nxt image');
	if(!$('#banner .banner-image').length) return false;
	$('#banner').each(function()
	{
		var c = $(this).data('currentImage');
		var n = c.next('.banner-image');
		if(!n.length) n = $(this).children('.banner-image').first();
		//if(!c.next('.drop-image').length) n = $(this).children('.drop-image:first-child');
		//else n = c.next('.drop-image');
		//console.log(n.attr('class'));
		c.fadeOut(700);
		n.fadeIn(700);
		$(this).data('currentImage',n);
		var num = parseInt(n.attr('id').replace('bi-',''));
		$('#banner .more').attr('src','/images/panel-arrow.png');
		if(num == 1 || num == 4)
		{
			// pulse first tab
			$('#tab-1 .more').attr('src','/images/pulse.gif');
		}
		if(num == 2 || num == 5)
		{
			// pulse second tab
			$('#tab-2 .more').attr('src','/images/pulse.gif');
		}
		if(num == 3 || num == 6)
		{
			// pulse second tab
			$('#tab-3 .more').attr('src','/images/pulse.gif');
		}
	});
	setTimeout('nextBannerImage()',5000)
}

var initSlider = function()
{
	var numSlides = $('.slider-option').length;
	var sliderWidth = $('#slider').width();
	var w =  sliderWidth / numSlides;
	expandedWidth = sliderWidth - ((numSlides-1) * 35);
	//console.log(w);

	$('.slider-option').each(function()
	{
		$(this).css('width',w);
		$(this).css('left',(w * sliderCount));
		$(this).data('openLeft',(35 * sliderCount));
		sliderCount++;
		$(this).data('closedLeft',(sliderWidth - (35 * sliderCount)));


		$(this).children('.slider-button').children('a').click(function()
		{
			selectSlide($(this).parent().parent());
		});

		if(sliderCount == numSlides) setTimeout('startSlides()',2000);
	});

}

var startSlides = function()
{
	selectSlide($('.slider-option').first());
}

var selectSlide = function(slide)
{
	//console.log('starting slider');
	clickedSlide = slide;
	var leftPos = 0;
	//if(currentSlide) currentSlide.animate({width:34,left:$(this).data('closedLeft')},500);
	//else {
		$('.slider-option').each(function()
		{
			if($(this).attr('id') != clickedSlide.attr('id')) {
				//console.log('closing');//$(this).animate({width:34,left:$(this).data('closedLeft')},500);
				$(this).addClass('closed');
				$(this).children('.slider-text').fadeOut();
				$(this).animate({width:34,left:leftPos},slideSpeed,'easeOutCirc');
				leftPos += 35;
			}
			else  {
				//console.log('opening');
				$(this).removeClass('closed');
				$(this).animate({width:expandedWidth,left:leftPos},slideSpeed,'easeOutCirc',function()
				{
					$(this).children('.slider-text').fadeIn();
				});
				leftPos += expandedWidth;
			}
		});
	//}
	//slide.animate({width:expandedWidth,left:$(this).data('openLeft')},500);
	currentSlide = slide;
}


function set_input_cifre(){
    $("input.numeric,input.cifre").keypress(function (e)
    {
        if (e.which==44 || e.which==46) if ($(this).val().indexOf('.')!=-1) return false;
        if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57) && (e.which!=46)) return false;
    });
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}


function verifica_contact(frm){
    $('#'+frm.id+' .mandator').removeClass('error');
    n = $('#'+frm.id+' .mandator').length;
    total_erori = 0;
    $('#'+frm.id+' .msg').removeClass('error').html('').hide();
    for (i=0; i<n; i++){
        obj = $('#'+frm.id+' .mandator:eq('+i+')');
        if ($(obj).val()==''){
            $(obj).addClass('error');
            $(obj).bind('keypress', function(e){$(this).removeClass('error');});
            total_erori++;
        }
    }
    if (total_erori){
        $('#'+frm.id+' .msg').html('Completati toate campurile').show();
        return false;
    }else if (!isValidEmailAddress($('#email').val())){
        $('#email').addClass('error');
        $('#email').bind('keypress', function(e){$(this).removeClass('error');});
        $('#'+frm.id+' .msg').html('Adresa de email este invalida').show();
        return false;
    }


    return true;
}

