var curent_header = 1;

$(document).ready(function(){    
 
	$('#gallery .lightbox').lightBox(); 
	
	jQuery('#carousel1').jcarousel({
        animation:500,
		scroll: 1,
 		auto: 3, 
 		wrap: 'last',	
		initCallback: mycarousel_initCallback 	 		
 	});
	
	jQuery('#carousel2').jcarousel({
        animation:500,
		scroll: 1, 		
 		wrap: 'last'		 		
 	});
    
    var carouselMare = $('.jcarousel-skin-tango:eq(0)');
    var carouselArrows = $('.jcarousel-prev, .jcarousel-next', carouselMare);
    carouselArrows.css('display', 'none');
    
    carouselMare.hover(
        function(){
            carouselArrows.show();        
        },
        function(){
            carouselArrows.hide();
        }  
    )
    
    
    
	$('#open-seo').click(function(){
		$('#text-seo-background').fadeIn('fast', function() {
			$('#text-seo').fadeIn('fast');
		});
	});
	$('#close-seo, #text-seo-background').click(function(){
		$('#text-seo').fadeOut('fast', function() {
			$('#text-seo-background').fadeOut('fast');
		});
	});

    $("#rez").click(function(){
        $.post(base+"/polls/view_reuslts",{},function(data){
            var parent = $(".quiz").parent().parent();
            $(parent).html(data);
            return false;
        });
    });       
    
    $('#email-login').hover(
        function(){
        $('#flashMessage').show();
        },
        function(){
        $('#flashMessage').hide();    
        }
    )
    
        
    $('.lightbox').lightBox();
    $('#galerie-mare a').lightBox();
          
    $('#galerie-mare a').mouseover(function(){
        $(".imagine-mare img").attr("src", this.rel);
        $(".imagine-mare img").attr("alt", this.title);
    })
    $(".expand-menu").click(function(){
    
        var element = $("#"+this.rel+">ul");
        if($(element).css("display")=='none') $("#"+this.rel+">ul").show();
        else $("#"+this.rel+">ul").hide();
    })
    
    
    $('textarea#InscrieteMesaj').click(function(){this.value = ""; this.style.color = '#454545';})
    
    sch(curent_header, 5);
    
    $(".header-number-button").click(function(){
        sch(this.rel, 5);
    })
    
    $(".header-prev-button").click(function(){
        if(curent_header==1) curent_header=5; else curent_header--;
        sch(curent_header, 5);
    })
    
    $(".header-next-button").click(function(){
        if(curent_header==5) curent_header=1; else curent_header++;
        sch(curent_header, 5);
    })
    
    /*On focus sterge email din casuta de user si daca nu baga nicio valoare scrie la loc */
    var userDefault = $("#email-login").val();
    $("#email-login").focus(function(event){
        if ($(this).val() == userDefault)
        {
            $(this).val('');
        }
    });
    $("#email-login").blur(function(event){
        if ($(this).val() == '')
        {
            $(this).val(userDefault);
        }
    });
    
    var passDefault = $("#pass-login").val();
    $("#pass-login").focus(function(event){
        if ($(this).val() == passDefault)
        {
            $(this).val('');
        }
    });
    $("#pass-login").blur(function(event){
        if ($(this).val() == '')
        {
            $(this).val(passDefault);
        }
    });
    
    $(".selected").mouseover(function(){
        var menu = $("#"+this.id+" > a");       
        $(".program-"+menu.attr("rel")).addClass("hoover");
    })
    
    $(".selected").mouseout(function(){
        var menu = $("#"+this.id+" > a");       
        $(".program-"+menu.attr("rel")).removeClass("hoover");
    })
    /********  ********/
    
    // embedded videos galleryes 
    var videos = $('embed', '#centercol');
    
    if(videos.length > 1){
        
        var urls = new Array();
            
        videos.each(function(){       
           urls.push($(this).attr("src")+'&autoplay=1');
           
           var tmp = $(this).attr("src").split('v/');
           var thumb_code = tmp[1].split('&hl');
           
           var video = new Array('<div>',
                        '<img alt="" src="http://i3.ytimg.com/vi/'+thumb_code[0]+'/default.jpg" alt="" />',                      
                        '</div>').join("");       
           $('#video-thumbs').append(video);                           
        });
        
        videos.remove();
        $('.page-text', '#centercol').prepend(videos[0]);
        
        
        
        
        $('#centercol br').remove();    
        $(videos[0]).attr("src", $(videos[0]).attr("src")+'&autoplay=1');   
        $('#video-thumbs div').eq(0).addClass('selected');        
        var thumbs = $('#video-thumbs div');    
        
        thumbs.each(function(i, item){
            $(this).click(function(){                       
                      thumbs.removeClass('selected');
                      $(this).addClass('selected');
                      $(videos[0]).attr("src", urls[i]);                   
            });                                                 
        });
    }
});

function arata(id) {

        var curr = 0;
        if(id==1) curr=5;
        else curr = id - 1;
        $("#carousel .show").fadeOut("fast", function() {
            $("#header-"+id +" .slide-but-"+curr).attr("src", "img/slide-"+curr+".png");
            $("#header-"+id +" .slide-but-"+curr).removeClass("curr");
            $("#carousel .show").removeClass("show");
            $(".visible").removeClass("visible");
            $("#header-"+id).fadeIn("fast");
            $("#header-"+id).addClass("show");
            $("#header-"+id +" .slide-but-"+id).addClass("curr");
            $("#header-"+id +" .slide-but-"+id).attr("src", "img/slide-"+id+"_over.png");
        });        
}



function sch(id,stop)
{
    arata(id);
    curent_header = id;
    if (id==stop) id=1;else id++;
    try
    {
    clearTimeout(masa);
    }
    catch(e)
    {
        
    }
    masa=setTimeout("sch('"+id+"','"+stop+"')",10000); 
}

function mycarousel_initCallback(carousel){
 
 	 // Disable autoscrolling if the user clicks the prev or next button.
	 carousel.buttonNext.bind('click', function() {
 	 	carousel.startAuto(0);
 	 });
 	 carousel.buttonPrev.bind('click', function() {
 	 	carousel.startAuto(0);
 	 });

	 // Pause autoscrolling if the user moves with the cursor over the clip.
 	 carousel.clip.hover(function() {
	    carousel.stopAuto();
 		}, function() {
 		carousel.startAuto();
 	 });

	 $('#gallery-thumbs a').click(function(){	 	
		carousel.scroll($(this).attr("rel"));
		carousel.startAuto(0);
		return false;
	 });
};