/* Error Message Handling */

// Get URL GET Paramters in an array
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
    
// Get the errorMess URL Parameter
var errorMess = getUrlVars()["errorMess"];
var tryBrand = getUrlVars()["tryBrand"];
var regStatus = getUrlVars()["reg"];
var logoutStatus = getUrlVars()["logout"];
var ifError = false;

// URL Decode this
errorMess = decodeURIComponent(errorMess);
tryBrand = decodeURIComponent(tryBrand);

/*
// Assign Brand Name
var brandName = "";
if (tryBrand == "lr") {
    brandName = "路虎"; 
    brandNameEN = "Land Rover";
}
if (tryBrand == "jag") { 
    brandName = "捷豹"; 
    brandNameEN = "Jaguar"; 
}
*/  



// Assign appropriate error message
if (errorMess == "undefined") errorMess = "";
else if(errorMess.indexOf("pwd") != -1) {
    errorMess = "用户名或密码认证不通过,请重试!<br />您要是没有捷豹路虎HUB帐号，请注册新帐号。<br />忘记密码请发<a style='color: #000;' href='mailto:admin@jaguarlandroverhub.com.cn'>admin@jaguarlandroverhub.com.cn</a>您的登录信息<br /><br />" +
        "User name or password error, please try again!<br />If you don't have a JLR Hub account yet, please register.<br />If you've forgotten your password, please email your account info to <a style='color: #000;' href='mailto:admin@jaguarlandroverhub.com.cn'>admin@jaguarlandroverhub.com.cn</a>.<br /><br />";
    ifError = true;
}
//else if(errorMess.indexOf("captcha") != -1) errorMess = "验证码不匹配,请检查一下!";
else errorMess = "";

if (regStatus == "ok") errorMess = "您的注册信息已提交，我们会在2个工作日内，进行权限审批。请注意查收您注册时的邮箱。<br /><br />Your registration info has been submitted. Within 2 working days, we will approve your account and you will receive a confirmation email. Thanks!";

if (logoutStatus == "true") errorMess = "谢谢您的来访！您已经成功退出，期待您的下次光临。<br /><br />You have logged out, thank you for visiting JLR Hub";

if (errorMess != "") setTimeout(function() {
    /*
    if (tryBrand == "lr") $('#lrSelect').trigger("click");
    if (tryBrand == "jag") $('#jaguarSelect').trigger("click");
    */
    $('#showError').trigger("click");
}, 500);
/* End Error Message Handling */



// Image Preloader
function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}




//Begin Jquery Actions on Page Load
$(document).ready(function(){ 
	   
    // Preload images
    preload(['/dealerportal/images/bg.jpg',
        '/dealerportal/images/popup1.png',
        '/dealerportal/images/popup2.png',
        '/dealerportal/images/popup1.gif',
        '/dealerportal/images/popup2.gif',
        '/dealerportal/loadingimg.gif'
        ]);

  
    // Set up any error messages 
    $('#errorMess').html(errorMess);
    $('#showError').fancybox();

    var selectBrand = $("#selectBrand");
    var jaguarLogin = $("#jaguarLogin");
    var lrLogin = $("#lrLogin");
    
    $(".transForm").hide(0);    
    selectBrand.hide(0);
    
    if(!ifError) selectBrand.fadeIn(1200).animate({
        opacity: .7
    });
    


    $('#lostpassBox').hide(0);
    
    var fadeTimeIn = 600;
	var fadeTimeOut = 350;
    
    var animateStyle = "drop";
    
    $('#jaguarSelect').click(function() {
        $('#jaguarForm').submit();
    });

            
    if(!ifError){
        // Hide appropriate forms on pageload for nicer animation
        $('#loginBox, #regBox').animate({
            left: "-700px"
        }, {
            duration: 0
        });
    
        // Slide in Login box from the left
        $( '#loginBox' ).animate({
            left: "0px"
        }, {
            duration: 1000
        });
    }

	
	
	
/*
    // Show jaguar login box
    $('#jaguarSelect').click(function(e) {
        e.preventDefault();
        
        if(selectBrand.is(":visible")) selectBrand.hide("puff");
        if(jaguarLogin.is(":hidden")) jaguarLogin.show(animateStyle, {
            direction: 'down'
        }, fadeTime);
        if(lrLogin.is(":visible")) lrLogin.hide(animateStyle, {
            direction: 'down'
        }, fadeTime);

        return false;
    });
	*/
	
	
    $('#jaguarSelect').hover(
        function(){
            jaguarLogin.show(animateStyle, {
            direction: 'down'
        }, fadeTimeIn)
		;
        },
        function(){
            jaguarLogin.hide(animateStyle, {
            direction: 'down'
        }, fadeTimeOut);
    
        });
    $('#lrSelect').hover(
        function(){
            lrLogin.show(animateStyle, {
            direction: 'down'
        }, fadeTimeIn)
		;
        },
        function(){
            lrLogin.hide(animateStyle, {
            direction: 'down'
        }, fadeTimeOut);
    
        });
		
    
}); 




    /*

    
   
    // Show LR login box
    $('#lrSelect').click(function(e) {
        e.preventDefault();
        
        if(selectBrand.is(":visible")) selectBrand.hide("puff");
        if(lrLogin.is(":hidden")) lrLogin.show(animateStyle, {
            direction: 'down'
        }, fadeTime);
        if(jaguarLogin.is(":visible")) jaguarLogin.hide(animateStyle, {
            direction: 'down'
        }, fadeTime);
        
        return false;
    });
    */

/*
 *
 *
    // Show/hide lost password field
    $('#lostpassLink').click(
        function() {
            $('#lostpassBox').slideToggle(500);
        } );

     // Slide out appropriate panels on clicking login
    $('#loginLink').click(function() {
        $('#bread1').text('登录');
        
        $('#loginLink').hide(0);
        $('#regLink').slideToggle(200);
        $('#regBox').animate({
            left: "-700px"
        }, {
            duration: 1000, 
            queue: false
        });
        $('#jaguarLogin').animate({
            left: "0px"
        }, {
            duration: 1000, 
            queue: false
        });
    });
    
    
    // Slide out appropriate panels on clicking register
    $('#regLink').click(function() {
        
        $('#bread1').text('注册');
        
        $('#regLink').hide(0);
        $('#loginLink').slideToggle(200);
        $('#jaguarLogin').animate({
            left: "-700px"
        }, {
            duration: 1000, 
            queue: false
        });
        $('#regBox').animate({
            left: "0px"
        }, {
            duration: 1000, 
            queue: false
        });
    });
    
 */
   /*

// Background Photo Shuffling
// Array Shuffler Function
shuffle = function(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

// Speed of the automatic slideshow
var slideshowSpeed = 5000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
    "image" : "bg5.jpg"
}, {
    "image" : "bg2.jpg"
},{
    "image" : "bg3.jpg"
}, {
    "image" : "bg4.jpg"
}
];

photos=shuffle(photos);
// End Background Photo shuffling functions
    var activeContainer = 1;	
    var currentImg = 0;
    var animating = false; 
    var navigate = function() {
        // Check if no animation is running. If it is, prevent the action
        if(animating) {
            return;
        }
		

        currentImg++;
        if(currentImg == photos.length + 1) {
            currentImg = 1;
        }

		
        // Check which container we need to use
        var currentContainer = activeContainer;
        if(activeContainer == 1) {
            activeContainer = 2;
        } else {
            activeContainer = 1;
        }
		
        showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
    };
	
    var currentZindex = -1;
    
    
    var showImage = function(photoObject, currentContainer, activeContainer) {
        animating = true;
		
        // Make sure the new container is always on the background
        currentZindex--;
		
        // Set the background image of the new active container
        $("#headerimg" + activeContainer).css({
            "background-image" : "url(images/" + photoObject.image + ")",
            "display" : "block",
            "z-index" : currentZindex
        });

		
        // Fade out the current container
        // and display the header text when animation is complete
        $("#headerimg" + currentContainer).fadeOut(function() {
            setTimeout(function() {
                $("#headertxt").css({
                    "display" : "block"
                });
                animating = false;
            }, 2000);
        });
    };
    
    // We should statically set the first image
    navigate("next");
	
    // Start playing the animation
    interval = setInterval(function() {
        navigate("next");
    }, slideshowSpeed);
*/


    /*
    // Make the log-in box shift on mouse-over
    $('#jaguarLogin').hover(
        function(){
            $(this).animate({
                left: "15px"
            }, {
                duration: 500
            });
        },
        function(){
            $(this).animate({
                left: "0px"
            }, {
                duration: 500
            });
    
        });
    */
