var currentMode = 'video';
var animationTime = '200';
var delayTime = '300';
var productId;
var tasks = new Array;
var animationStarted = false;

function isValidEmail(strEmail){
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(strEmail)) {
        return false;
    }
    return true;
}

var lang = null;

$.getJSON("http://" + document.domain + "/language/load-json-dictionary/", function(data){
    lang = data;
});


function addUser($invalidEmail, $exists, $thanks){
    $email = $('#newsletterMail').attr("value");
    if(isValidEmail($email)){
        $("#newsletterLabel").html('<img src="/images/admin/ajax-loader.gif" alt="" id="reloader" />');
        $.ajax({
                type: "POST",
                url: "/newsletter/add-user",
                data: "email=" + $email ,
                success: function(msg){
                    $("#reloader").hide();
                    if(msg == 'exists')
                        $("#newsletterLabel").html('<b>'+lang.email_already_exists+'</b>');
                    else if(msg == 'ok')
                        $("#newsletterLabel").html('<b><font color="green">'+lang.thanks+' !</font></b>');
                }
        });
    }else{
        $("#newsletterLabel").html('<b>'+lang.invalid_email+'</b>');
    }
}

function loadVideoMode()
{
    
	if(FlashDetect.installed == false || $.cookie('non_flash') == 1)
	{
	    $("#slider-widget").animate({ width:'220', top:'0', height:'190', right:'240' }, animationTime);
	    $("#products-widget").animate({ width:'220', top:'191', height:'158', right:'240' }, animationTime);
	    $("#video-widget,#openLink").animate({ width:'460', height:'370' }, animationTime, function(){});
	    $("#openLink img").animate({ width:'460', height:'256',paddingTop:'60px' }, animationTime, function(){});
	    
	    $(".optional-news").fadeOut(animationTime);
	    $("#news-widget").animate({ top:'390', height:'195' }, animationTime);
	    $("#newsletter-widget").animate({ width:'220' }, animationTime);
	    $("#mobile-widget").animate({ width:'220', left:'480' }, animationTime);
	    $("#paper-widget").animate({ width:'220', left:'720' }, animationTime);
	    $("#promotion-text").fadeIn(animationTime);
	    $(".product-home").hide();
	    productFlashOn();
	}
	else
	{
	    $("#slider-widget").animate({ width:'220', top:'0', height:'190', right:'240' }, animationTime);
	    $("#products-widget").animate({ width:'220', top:'191', height:'158', right:'240' }, animationTime);
	    $("#video-widget").animate({ width:'460', height:'370' }, animationTime, function(){
	        $("#video-widget object").attr('width', '460');
	        $("#video-widget object").attr('height', '373');
	    });
	    $(".optional-news").fadeOut(animationTime);
	    $("#news-widget").animate({ top:'390', height:'195' }, animationTime);
	    $("#newsletter-widget").animate({ width:'220' }, animationTime);
	    $("#mobile-widget").animate({ width:'220', left:'480' }, animationTime);
	    $("#paper-widget").animate({ width:'220', left:'720' }, animationTime);
	    $("#promotion-text").fadeIn(animationTime);
        $(".product-home").hide();

	    productFlashOn();
	}
    $("#all-products-widget").animate({ width:'220' }, animationTime);
    $("#all-products-widget img").animate({ height:'370' }, animationTime);
    
    $("#slider-footer").show();
	
    $(".product-home-header").animate({height:'0'}, animationTime);
}

function loadSliderMode()
{
    //console.log('sliderMode');
    
	if(FlashDetect.installed == false || $.cookie('non_flash') == 1)
	{
	    
	    $("#slider-widget").animate({ width:'460', top:'0', height:'190', right:'240' }, animationTime);
	    $("#products-widget").animate({ width:'460', top:'191', height:'158', right:'240' }, animationTime);
	    $("#openLink").animate({ width:'220', height:'150' }, animationTime, function(){});
	    $("#openLink img").animate({ width:'220', height:'120',paddingTop:'25px' }, animationTime, function(){});
	    
	    
	    $("#video-widget").animate({ width:'220', height:'168' }, animationTime, function(){});
	    
	    $(".optional-news").fadeIn(animationTime);
	    $("#news-widget").animate({ top:'200', height:'385' }, animationTime);
	    $("#newsletter-widget").animate({ width:'220' }, animationTime);
	    $("#mobile-widget").animate({ width:'220', left:'480' }, animationTime);
	    $("#paper-widget").animate({ width:'220', left:'720' }, animationTime);
	    $("#promotion-text").fadeOut(animationTime);
	}
	else
	{
	    $("#slider-widget").animate({ width:'460', top:'0', height:'190', right:'240' }, animationTime);
	    $("#products-widget").animate({ width:'460', top:'191', height:'158', right:'240' }, animationTime);
	    $("#video-widget").animate({ width:'220', height:'188' }, animationTime, function(){
	        $("#video-widget object").attr('width', '220');
	        $("#video-widget object").attr('height', '188');
	    });
	    $(".optional-news").fadeIn(animationTime);
	    $("#news-widget").animate({ top:'200', height:'385' }, animationTime);
	    $("#newsletter-widget").animate({ width:'220' }, animationTime);
	    $("#mobile-widget").animate({ width:'220', left:'480' }, animationTime);
	    $("#paper-widget").animate({ width:'220', left:'720' }, animationTime);
	    $("#promotion-text").fadeOut(animationTime);
    }
 
    $("#all-products-widget").animate({ width:'220' }, animationTime);
    $("#all-products-widget img").animate({ height:'370' }, animationTime);

    $("#slider-footer").show();

	//$(".product-home").fadeOut(animationTime);
    $(".product-home-header").animate({height:'0'}, animationTime);
    $("#product-home-"+productId).fadeIn(animationTime);
    productFlashOff();

}

function loadProductsMode()
{
    //console.log('productsMode');
	
	if(FlashDetect.installed == false || $.cookie('non_flash') == 1)
	{
	    $("#slider-widget").animate({ width:'460', height:'138', right:'240' }, animationTime);
	    $("#products-widget").animate({ width:'460', top:'139', height:'210', right:'240' }, animationTime);
	    $("#video-widget").animate({ width:'220', height:'168' }, animationTime, function(){});
	    $("#video-widget #openLink img").animate({ width:'220', height:'120',paddingTop:'25px'  }, animationTime, function(){});
	    $("#video-widget #openLink").animate({ width:'220', height:'150' }, animationTime, function(){});
	    $(".optional-news").fadeIn(animationTime);
	    $("#news-widget").animate({ top:'200', height:'385' }, animationTime);
	    $("#newsletter-widget").animate({ width:'220' }, animationTime);
	    $("#mobile-widget").animate({ width:'220', left:'480' }, animationTime);
	    $("#paper-widget").animate({ width:'220', left:'720' }, animationTime);
	    $("#promotion-text").fadeOut(animationTime);
    }
	else
	{
	    $("#slider-widget").animate({ width:'460', height:'138', right:'240' }, animationTime);
	    $("#products-widget").animate({ width:'460', top:'139', height:'210', right:'240' }, animationTime);
	    $("#video-widget").animate({ width:'220', height:'188' }, animationTime, function(){
	        $("#video-widget object").attr('width', '220');
	        $("#video-widget object").attr('height', '188');
	    });
	    $(".optional-news").fadeIn(animationTime);
	    $("#news-widget").animate({ top:'200', height:'385' }, animationTime);
	    $("#newsletter-widget").animate({ width:'220' }, animationTime);
	    $("#mobile-widget").animate({ width:'220', left:'480' }, animationTime);
	    $("#paper-widget").animate({ width:'220', left:'720' }, animationTime);
	    $("#promotion-text").fadeOut(animationTime);
	}

    $("#all-products-widget").animate({ width:'220' }, animationTime);
    $("#all-products-widget img").animate({ height:'370' }, animationTime);
    
	$("#product-home-"+productId).fadeIn(animationTime);
    $(".product-home-header").animate({height:'49'}, animationTime).fadeIn();
    
    $("#slider-footer").hide();
    
	productFlashOff();
	
//    var productsArray = $(".product-home");
//    var randomNum = Math.floor( Math.random() * productsArray.length + 1 );
//    productsArray.eq(randomNum - 1).fadeIn(animationTime);
//    productFlashOff();      
 
}

function loadAllProductsMode()
{
    if(FlashDetect.installed == false || $.cookie('non_flash') == 1)
    {
        $("#slider-widget").animate({ width:'400', height:'190', right:'300' }, animationTime);
        $("#products-widget").animate({ width:'400', top:'191', height:'158', right:'300' }, animationTime);
        $("#video-widget").animate({ width:'220', height:'168' }, animationTime, function(){});
        $("#video-widget #openLink img").animate({ width:'220', height:'120',paddingTop:'25px'  }, animationTime, function(){});
        $("#video-widget #openLink").animate({ width:'220', height:'150' }, animationTime, function(){});
        $(".optional-news").fadeIn(animationTime);
        $("#news-widget").animate({ top:'200', height:'385' }, animationTime);
        $("#newsletter-widget").animate({ width:'220' }, animationTime);
        $("#mobile-widget").animate({ width:'220', left:'480' }, animationTime);
        $("#paper-widget").animate({ width:'220', left:'720' }, animationTime);
        $("#promotion-text").fadeOut(animationTime);
    }
    else
    {
        $("#slider-widget").animate({ width:'400', height:'190', right:'300' }, animationTime);
        $("#products-widget").animate({ width:'400', top:'191', height:'158', right:'300' }, animationTime);
        $("#video-widget").animate({ width:'220', height:'188' }, animationTime, function(){
            $("#video-widget object").attr('width', '220');
            $("#video-widget object").attr('height', '188');
        });
        $(".optional-news").fadeIn(animationTime);
        $("#news-widget").animate({ top:'200', height:'385' }, animationTime);
        $("#newsletter-widget").animate({ width:'220' }, animationTime);
        $("#mobile-widget").animate({ width:'220', left:'480' }, animationTime);
        $("#paper-widget").animate({ width:'220', left:'720' }, animationTime);
        $("#promotion-text").fadeOut(animationTime);
    }

    $("#all-products-widget").animate({ width:'280' }, animationTime);
    $("#all-products-widget img").animate({ height:'437' }, animationTime);
    
    $("#slider-footer").show();

    $(".product-home").hide();
    $(".product-home-header").animate({height:'0'}, animationTime);
    productFlashOn();
}

function loadNewsletterMode()
{
    $("#slider-widget").animate({ top:'0', height:'190'}, animationTime);
    $("#products-widget").animate({ top:'191', height:'158' }, animationTime);

    $("#newsletter-widget").animate({ width:'280' }, animationTime);
    $("#mobile-widget").animate({ width:'190', left:'540' }, animationTime);
    $("#paper-widget").animate({ width:'190', left:'750' }, animationTime);
    
    $(".product-home").hide();
    $(".product-home-header").animate({height:'0'}, animationTime);
    $("#slider-footer").show();
    productFlashOn();
}

function loadMobileMode()
{
    $("#slider-widget").animate({ top:'0', height:'190'}, animationTime);
    $("#products-widget").animate({ top:'191', height:'158' }, animationTime);

    $("#newsletter-widget").animate({ width:'190' }, animationTime);
    $("#mobile-widget").animate({ width:'280', left:'450' }, animationTime);
    $("#paper-widget").animate({ width:'190', left:'750' }, animationTime);

    $(".product-home").hide();
    $(".product-home-header").animate({height:'0'}, animationTime);
    $("#slider-footer").show();
    productFlashOn();
}

function loadPaperMode()
{
    $("#slider-widget").animate({ top:'0', height:'190' }, animationTime);
    $("#products-widget").animate({ top:'191', height:'158' }, animationTime);

    $("#newsletter-widget").animate({ width:'190' }, animationTime);
    $("#mobile-widget").animate({ width:'190', left:'450' }, animationTime);
    $("#paper-widget").animate({ width:'280', left:'660' }, animationTime);

    $(".product-home").hide();
    $(".product-home-header").animate({height:'0'}, animationTime);
    $("#slider-footer").show();
    productFlashOn();	
}

var timeoutProductFlashOn = false;

function productFlashOn(flashSize)
{
	if ( timeoutProductFlashOn )
	{
		clearTimeout( timeoutProductFlashOn );
	}
	timeoutProductFlashOn = setTimeout( 'productFlashOn2();', 400 );
	
}

function productFlashOn2(flashSize)
{
    if((currentMode == 'mobile' || currentMode == 'paper') && $("#products-widget").width() == '460')
    {
        //return false;
    }
    
    var w = $("#products-widget").width();

    if( w >= 400 )
    {
        if(!$("#flashnav").hasClass("bigFlash") /*&& !$("#promotion-text").is(":visible")*/)
        {   
        	if(FlashDetect.installed == false || $.cookie('non_flash') == 1)
        	{
        		 var m = ( noneSwfProductsBig == '' ? '/images/all_products.png' : noneSwfProductsBig );
        		
        		$("#flashnav").html('<img src="' + m + '" width="410">').addClass("noflash");
        	}
        	else
        	{
        		
               var m = ( swfProductsBig == '' ? '/flash2.swf' : swfProductsBig );
        		
                var fo = new FlashObject( m, "flash", "410", "160", "7", "#727272");
                fo.addParam("menu", "false");
                fo.addParam("quality", "high");
                fo.addParam("wmode", "transparent");
                fo.addParam("flashvars", "");
                fo.write("flashnav");       		
        	}
        	
            $("#flashnav").addClass("bigFlash").removeClass("smallFlash");
        }
    }
    else
    {
        if(!$("#flashnav").hasClass("smallFlash"))
        {
        	if(FlashDetect.installed == false || $.cookie('non_flash') == 1)
        	{
        		var m = ( noneSwfProductsSmall == '' ? '/images/some_products.png' : noneSwfProductsSmall );
        		$("#flashnav").html('<img src="' + m + '" width="240">');
        	}
        	else
        	{
        		var m = ( swfProductsSmall == '' ? '/flash.swf' : swfProductsSmall );
        		
                var fo = new FlashObject(m, "flash", "230", "160", "7", "#727272");
                fo.addParam("menu", "false");
                fo.addParam("quality", "high");
                fo.addParam("wmode", "transparent");
                fo.addParam("flashvars", "");
                fo.write("flashnav");        		
        	}

            $("#flashnav").addClass("smallFlash").removeClass("bigFlash");
        }
    }
        
}

function productFlashOff()
{
    $("#flashnav").html("");
    $("#flashnav").removeClass("bigFlash");
    $("#flashnav").removeClass("smallFlash");
}

function addToQueue(task)
{
    tasks.push(task);
    
//    if(tasks.length < 3)
//    {
        if(task == 'slider'){
            loadSliderMode();
        }else if(task == 'video'){
            loadVideoMode();
        }else if(task == 'video'){
            loadVideoMode();
        }else if(task == 'products'){
            loadProductsMode();
        }else if(task == 'news'){
            loadSliderMode();
        }else if(task == 'newsletter'){
            loadNewsletterMode();
        }else if(task == 'mobile'){
            loadMobileMode();
        }else if(task == 'paper'){
            loadPaperMode();
        }else if(task == 'all-products'){
            loadAllProductsMode();
        }
        
        //setTimeout("tasks = new Array;", 500);
        //console.log(tasks);
//    }
}

function flickerAnimation()
{
    $("#blue2-mask").fadeOut(1000, function(){
        $("#blue2-mask").fadeIn(1000);
    });
    setTimeout(flickerAnimation, 2000);

}

$(document).ready(function() {
   
    var string = "http://nl.hg.eu/products/show/id/4";
    productId = $(".product-home").eq(0).attr("id").replace("product-home-", "");
    $('#slider').nivoSlider({
        pauseTime: 7000,
        animSpeed: 500,
        beforeChange: function(){
            if(currentMode == 'products' || currentMode == 'slider')
            {
                $(".product-home").fadeOut(animationTime);
            }
        },
        afterChange: function(){
            var productUrl = $("#slider .slider_link:visible").attr('href');
            productIdMatch = productUrl.match(/\/show\/id\/(\d+)/);
            productId = productIdMatch[1];
            if(currentMode == 'products' || currentMode == 'slider')
            {
                $(".product-home-header h3 span").html($("#product-home-"+productId+" .pr_it").html());
                $("#product-home-"+productId).fadeIn(animationTime);
            }
        }
    });
    
    productFlashOn();
    
    flickerAnimation();
    
    $(".news-item").tooltip({
        showURL: false, 
        bodyHandler: function() { 
            return $(this).find('.preview').html(); 
        }
    });
    
    $("#newsletter-widget span .button").live("click", function(){
        addUser();
    });
    
    $(".widget").mouseover(function() {
        $(this).addClass('effect');
      }).mouseout(function(){
          $(this).removeClass('effect');
          $("#flashnav").mouseout();
    });
    
    $("#slider-widget").live("mouseover", function(event){
        if ( event.type == "mouseover" && currentMode != 'slider') {
            addToQueue('slider');
            currentMode = 'slider';
            //loadSliderMode();
        }
    });

    $("#video-widget").live("mouseover", function(event){
        if ( event.type == "mouseover" && currentMode != 'video') {
            addToQueue('video');
            currentMode = 'video';
            //loadVideoMode();
            $("#flashnav").mouseout();
        }
    });

    $("#products-widget").live("mouseover", function(event){
        if ( event.type == "mouseover" && currentMode != 'products') {
            addToQueue('products');
            currentMode = 'products';
            //loadProductsMode();
            $("#flashnav").mouseover();
        }
    });
    
    $("#news-widget").live("mouseover", function(event){
        if ( event.type == "mouseover" && currentMode != 'slider') {
            addToQueue('slider');
            currentMode = 'slider';
            //loadSliderMode();
            $("#flashnav").mouseout();
        }
    });
    $("#newsletter-widget").live("mouseover", function(event){
        if ( event.type == "mouseover" && currentMode != 'newsletter') {
            addToQueue('newsletter');
            currentMode = 'newsletter';
            //loadNewsletterMode();
        }
    });

    $("#mobile-widget").live("mouseover", function(event){
        if ( event.type == "mouseover" && currentMode != 'mobile') {
            addToQueue('mobile');
            currentMode = 'mobile';
            //loadMobileMode();
        }
    });

    $("#paper-widget").live("mouseover", function(event){
        if ( event.type == "mouseover" && currentMode != 'paper') {
            addToQueue('paper');
            currentMode = 'paper';
            //loadPaperMode();
        }
    });
    $("#all-products-widget").live("mouseover", function(event){
        if ( event.type == "mouseover" && currentMode != 'all-products') {
            addToQueue('all-products');
            currentMode = 'all-products';
            //loadAllProductsMode();
        }
    });

    
});
