var mainURL = "http://www.";
var catalogURL1 = "MK_FW2011_final.pdf";
var catalogURL2 = "MK_SS2012_final.pdf";
var backgrounds = new Array();
var currentImage = 0;
var screenX = 0;
var screenY = 0;
var minX = 0;
var minY = 0;
var imageX = 0;
var imageY = 0;
var currentX = 0;
var currentY = 0;
var jBI;
var toggle = false;
var isMobile = false;
var isIPhone = false;
var pause = false;
function initialize() {
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/series60/i)) || (navigator.userAgent.match(/symbian/i)) || (navigator.userAgent.match(/android/i)) || (navigator.userAgent.match(/windows ce/i)) || (navigator.userAgent.match(/blackberry/i)) || (navigator.userAgent.match(/palm/i))) isMobile = true;
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) isIPhone = true;
    jBI = $("#backgroundImageA");
    jBIAlt = $("#backgroundImageB");
    loadBackgrounds();
    preloadImages();
    rotateImage();
}
function loadBackgrounds() {
    if (isMobile) {
        //backgrounds[0] = ["MKlandingpage_BG1_iphone.jpg", 480, 320, ""];
        //backgrounds[1] = ["MKlandingpage_BG2_iphone.jpg", 480, 320, ""];
        //backgrounds[2] = ["MKlandingpage_BG3_iphone.jpg", 480, 320, ""];
        backgrounds[0] = ["MKlandingpage_BG4_iphone.jpg", 480, 320, ""];
        minX = 480;
        minY = 320;
        imageX = backgrounds[currentImage][1];
        imageY = backgrounds[currentImage][2];
        if (isIPhone) document.title = "maryamkeyhani.com - iPhone";
        else document.title = "maryamkeyhani.com - mobile";
    } else {
        //backgrounds[0] = ["MKlandingpage_BG1.jpg", 1024, 675, ""];
        //backgrounds[1] = ["MKlandingpage_BG2.jpg", 1024, 675, ""];
        //backgrounds[2] = ["MKlandingpage_BG3.jpg", 1024, 675, ""];
        backgrounds[0] = ["MKlandingpage_BG4.jpg", 1024, 675, ""];
        minX = 700;
        minY = 400;
        imageX = backgrounds[currentImage][1];
        imageY = backgrounds[currentImage][2];
        $(window).resize(resizeImage);
    }
}
function preloadImages() {
    for (var i = 0; i < backgrounds.length; i++) {
        backgrounds[i][3] = new Image(backgrounds[i][1], backgrounds[i][2]);
        backgrounds[i][3].src = backgrounds[i][0];
    }
}
function resetVariables() {
    screenX = $("body").width();
    screenY = $("body").height();
    if (screenX < minX) { screenX = minX; }
    if (screenY < minY) { screenY = minY; }
    currentX = jBI.attr("width");
    currentY = jBI.attr("height");
}
function rotateImage() {
    if (pause) return;
    pause = true; //Hack for only one image
    if (currentImage < backgrounds.length - 1) { currentImage += 1; }
    else { currentImage = 0; }
    if (toggle) {
        toggle = false;
        jBI = $("#backgroundImageA");
        jBIAlt = $("#backgroundImageB");
    }
    else {
        toggle = true;
        jBI = $("#backgroundImageB");
        jBIAlt = $("#backgroundImageA");
    }
    jBI.attr("src", backgrounds[currentImage][0]);
    if (isMobile) {
        resizeImage();
        if (isIPhone) {
            jBIAlt.css("opacity", "0");
            jBI.css("opacity", "1");
            jBIAlt.css("-webkit-transition", "opacity 1s linear");
            jBI.css("-webkit-transition", "opacity 1s linear");
        } else {
            jBIAlt.fadeOut(1000);
            jBI.fadeIn(1000);
        }
    } else {
        resizeImage();
        jBIAlt.animate({ marginLeft: "-5px", opacity: 0 }, 1000);
        jBI.animate({ marginLeft: "0px", opacity: 1 }, 1000);
    }
    window.setTimeout(rotateImage, 5000);
}
function resizeImage() {
    resetVariables();
    var ratioImage = imageX / imageY;
    var ratioScreen = screenX / screenY;
    if (ratioImage > ratioScreen) {
        jBI.attr("height", screenY);
        jBI.attr("width", Math.ceil(screenY / imageY * imageX));
    } else {
        jBI.attr("width", screenX);
        jBI.attr("height", Math.ceil(screenX / imageX * imageY));
    }
    centerImage();
}
function centerImage() {
    resetVariables();
    if (currentX > screenX) { jBI.css("left", Math.ceil((screenX - currentX) / 2)); }
    else { jBI.css("left", 0); }
    if (currentY > screenY) { jBI.css("top", Math.ceil((screenY - currentY) / 2)); }
    else { jBI.css("top", 0); }
}
function pauseRotation() {
    return; //Hack for single background
    pause = true;
}
function unpauseRotation() {
    return; //Hack for single background
    pause = false;
    window.setTimeout(rotateImage, 5000);
}

function downloadCatalog() {
    window.open(catalogURL1, 'Download');
}

function downloadSecureCatalog() {
    pauseRotation();
    if ($.browser.msie) {
        $("#transparentBox").css("display", "block");
    } else {
        $("#transparentBox").fadeIn('fast');
    }
    $("#login").fadeIn('fast');
    $("#loginSuccess").fadeOut('fast');
    $("#loginFail").fadeOut('fast');
}

function checkPassword() {
    if ($("#username").attr("value").toLowerCase() == "mkss12" && $("#password").attr("value").toLowerCase() == "mkss12") {
        $("#login").fadeOut('fast');
        $("#loginSuccess").fadeIn('fast');
        $("#loginFail").fadeOut('fast');
        window.open(catalogURL2, 'Download');
    } else {
        $("#login").fadeOut('fast');
        $("#loginSuccess").fadeOut('fast');
        $("#loginFail").fadeIn('fast');
    }
}
function unfadeSite() {
    if ($.browser.msie) {
        $("#transparentBox").css("display", "none");
    } else {
        $("#transparentBox").fadeOut('fast');
    }
    $("#login").fadeOut('fast');
    $("#loginSuccess").fadeOut('fast');
    $("#loginFail").fadeOut('fast');
    unpauseRotation();
}
$(document).ready(initialize);

