// 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 (_gaq)
    {
        _gaq.push(['_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 (_gaq) {
        _gaq.push(['_trackEvent',category, action, label, value]);
    }
}

function CustomJSEnabled(pageUrl) {
    $.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",
            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);
            }
        });
    }
};


/*******************************************************************
SocialMediaFunctions called from Flash.
This function is needed because Pagetype markup is not present when use navigates in flash. 
Only the landingpage markup is presented.
********************************************************************/
//TWITTER
var sharer;
var Share = {

    Twitter: function(u,t,pageid,brandname) {
    
        sharer = window.open("/lib/commonassets/LoadingTwitter.html" ,"sharer","toolbar=0,status=0,width=800,height=436");
        //Open window on click to prevent popup-blocker kicking in.
        //When the webservice returns the bitly-url, redirect "sharer" to the correct address.        
        $.ajax({
            type: "POST",
            url: "/Web/Services/SocialMediaService.asmx/Google",
            data: "{\"u\":\"" + u + "\", \"pageid\":\"" + pageid + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) 
            {
                var bit = msg["d"];
		        sharer.location.href = "http://twitter.com/home?status=See%20what%20I%20found%20on%20"+brandname+"%20 ["+encodeURIComponent(bit)+"]";
	            if (_gaq) {
	                _gaq.push(['_trackEvent','sharing', 'twitter', u]);
                }
            }
        });
    },
    Email: function(u,t,pageid,brandname) {
        $.ajax({
            type: "POST",
            url: "/Web/Services/SocialMediaService.asmx/EMailHtml",
            data: "{\"brandname\":\"" + brandname + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) 
            {
	            if (_gaq) {
	                _gaq.push(['_trackEvent','sharing', 'email to friend', u]);
                }
            }
        });
     }
};

//FACEBOOK
function fbs_click_fromFlash(u,t,pageid, brandname)
{     
    if(u==undefined && t==undefined)
        return;
        
    var fbString = "http://www.facebook.com/sharer.php?u="+encodeURIComponent(u)+"&t="+encodeURIComponent(t);
    window.open(fbString,"sharer","toolbar=0,status=0,width=626,height=436");
	
	if (_gaq) {
	    _gaq.push(['_trackEvent','sharing', 'facebook', u]);
    }
}

//EMAIL TO FRIEND

/********************************************************************
********************************************************************/


// 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;
        }
    });

});
