// Tell flash that JS is ready to receive calls
var jsReady = false;
function isReady() {
    return jsReady; 
}

function pageInit() {
    jsReady = true;
}

var setPageTitle = function(title) {
    document.title = title;
};

// Called by Flash when a page is changed
var registerPageLoad = function(url, title) 
{
    $.history.load(url.replace(/^.*#/, ''));
    if (title)
    {
        setPageTitle(title);
    }
};

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        if (window[movieName] == undefined)
            return document.forms["aspnetForm"][movieName];
        return window[movieName];
    } else {
        return document[movieName];
    }
}

// Call Google Analytics to track url
function trackPageView(url)
{
    if (pageTracker)
    {
        pageTracker._trackPageview(url);
    }
}

// Called by JavaScript when the page is loaded or back/forward clicked
var firstPageChange = true;
var callPageChange = function(url) {
    if (!url) {
        url = "/";
    }
    if (!firstPageChange) {
        trackPageView(url);
        thisMovie("mainFlash").sendToActionScript(url);
    }
    if (firstPageChange && window.location.href.indexOf("#") > -1) {
        url = window.location.href.substring(window.location.href.indexOf("#") + 1, window.location.href.length);
        $(function() {
            setTimeout("thisMovie(\"mainFlash\").sendToActionScript(\"" + url + "\")", 200);
        });
    }
    firstPageChange = false;
};

// Browser safe way to get the flash movie
var getMainFlash = function() {
    return thisMovie("mainFlash");
};

function trackEvent(category, action, label, value) {
    if (pageTracker) {
		// alert(category + "\n"+action+"\n"+label+"\n"+value);
        pageTracker._trackEvent(category, action, label, value);
    }
}

function CustomJSEnabled(pageUrl) {
    alert("customJS enabled");
    $.ajax({
        type: "GET",
        url: pageUrl + "?format=js",
        success: function(msg) {
            eval(msg);
        }
    });
}

var Wishlist = {
    AddItem: function(pageUrl) {
        $.ajax({
            type: "POST",
            url: "/Web/Services/WishlistService.asmx/AddItem",
            data: "{\"pageUrl\":\"" + pageUrl + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                thisMovie("mainFlash").updateWishlist(msg);
            }
        });
    },
    RemoveItem: function(pageUrl) {
        $.ajax({
            type: "POST",
            url: "/Web/Services/WishlistService.asmx/RemoveItem",
            data: "{\"pageUrl\":\"" + pageUrl + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                getMainFlash().updateWishlist(msg);
            }
        });
    },
    Comment: function(pageUrl, comment) {
        $.ajax({
            type: "POST",
            url: "/Web/Services/WishlistService.asmx/Comment",
            data: "{\"pageUrl\":\"" + pageUrl + "\", \"comment\":\"" + comment + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                getMainFlash().commentStored(msg);
            }
        });
    },
    Send: function(fromName, fromMail, toName, toMail, comment) {
        $.ajax({
            type: "POST",
            url: "/Web/Services/WishlistService.asmx/Send",
            data: "{\"fromName\":\"" + fromName + "\", \"fromMail\":\"" + fromMail + "\", \"toName\":\"" + toName + "\", \"toMail\":\"" + toMail + "\", \"comment\":\"" + comment + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                getMainFlash().mailSentSuccess(msg);
            }
        });
    },
    CountItems: function() {
        $.ajax({
            type: "POST",
            url: "/Web/Services/WishlistService.asmx/CountItems",
            data: "{\"fromName\":\"" + fromName + "\", \"fromMail\":\"" + fromMail + "\", \"toName\":\"" + toName + "\", \"toMail\":\"" + toMail + "\", \"comment\":\"" + comment + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                getMainFlash().updateWishlist(msg);
            }
        });
    },
    CheckForItem: function(pageurl) {
      $.ajax({
            type: "POST",
            url: "/Web/Services/WishlistService.asmx/CheckForItem",
            data: "{\"pageurl\":\"" + pageurl + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                getMainFlash().itemIsOnList(msg);
            }
        });
    }
};

// Events
$(function() { // on dom ready
    pageInit();
    $("#page").removeClass("scriptDisabled").addClass("scriptEnabled");
    if (window.enableFlashFrontend)
    {
        $.history.init(callPageChange);
    }

    // Dropdowns on ShopList page
    $("body.ShopList select, body.Shop select").bind("change", function() {
        document.location = $(this).val();
    });

    // Language Menu
    $("#languageSelector").hover(function() {
        $("#languageSelectionMenu").css("display", "block");
    }, function() {
        $("#languageSelectionMenu").css("display", "none");
    });

    //isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
    //if (isIE6) {
    var widthLocked = false;
    var heightLocked = false;
    if (!widthLocked && $("body").width() < 960) {
        //$("#page, #container").css("width", "960px");
        $("#mainFlash").attr("width", "960");
        widthLocked = true;
    }
    if (!heightLocked && $("body").height() < 576) {
        //$("#page, #container").css("height", "576px");
        $("#mainFlash").attr("height", "576");
        heightLocked = true;
    }
    $(window).bind("resize", function() {
        if (!widthLocked && $("body").width() < 960) {
            //$("#page, #container").css("width", "960px");
            $("#mainFlash").attr("width", "960");
            widthLocked = true;
        } else if (widthLocked && $("body").width() > 960) {
            //$("#page, #container").css("width", "100%");
            $("#mainFlash").attr("width", "100%");
            widthLocked = false;
        }
        if (!heightLocked && $("body").height() < 576) {
            //$("#page, #container").css("height", "576px");
            $("#mainFlash").attr("height", "576");
            heightLocked = true;
        } else if (heightLocked && $("body").height() > 576) {
            //$("#page, #container").css("height", "100%");
            $("#mainFlash").attr("height", "100%");
            heightLocked = false;
        }
    });

});