﻿$(document).ready(function () {
   UpdateWishListItemCount();
    //*\\START Navigation Rollovers//*\\
    $(".SubNav img").each(function () {
        // Set the original src
        rollsrc = $(this).attr("src");
        ext = rollsrc.substring(rollsrc.lastIndexOf('.'));
        rollON = rollsrc.replace(ext, "_on" + ext);
        $("<img>").attr("src", rollON);
    });
    // Navigation rollovers
    $(".SubNav a").mouseover(function () {
        imgsrc = $(this).children("img").attr("src");
        if (imgsrc != null) {
            ext = imgsrc.substring(imgsrc.lastIndexOf('.'));
            var ndx = imgsrc.search(/_on/);
            // don't do the rollover if state is already ON
            if (ndx == -1) {
                imgsrcON = imgsrc.replace(ext, "_on" + ext); // strip off extension
                $(this).children("img").attr("src", imgsrcON);
            }
        }
    });
    $("#.SubNav a").mouseout(function () {
        $(this).children("img").attr("src", imgsrc);
    });
    //*\\END Navigation Rollovers//*\\


    //*\\START Popup Window//*\\
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(document).width();
        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
        //transition effect		
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("slow", 0.4);
        //Set the popup window to center
        var winH = $(window).height();
        var winW = $(window).width();
        $('#dialog').css('top', winH / 2 - $('#dialog').height() / 2);
        $('#dialog').css('left', winW / 2 - $('#dialog').width() / 2);
        //Set the popup window to center
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);
        //transition effect
        $(id).fadeIn(2500);
    });
    //if close button is clicked
    $('.window .close').live('click', function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('.window').fadeOut(1000);
        $('#mask').fadeOut(1000);
    });
    //if mask is clicked
    $('#mask').click(function () {
        $('.window').fadeOut(1000);
        $(this).fadeOut(1000);
    });

    //*\\END Popup Window//*\\
});

$.fn.showFeatureText = function () {
    return this.each(function () {
        var box = $(this);
        var text = $('.FlyOverContainer', this);
        text.hide();
        box.hover(function () {
            text.stop(true, true).slideDown("slow");
        }, function () {
            text.stop(true, true).slideUp("slow");
        });
    });
}
//Open Radwindow
function OpenWin(what, name) {
    var oWnd = radopen(what, name);
}

function OpenWindow() {
    var maskHeight = $(document).height();
    var maskWidth = $(document).width();
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo("slow", 0.4);
    var winH = $(window).height();
    var winW = $(window).width();
    $('#dialog').css('top', winH / 2 - $('#dialog').height() / 2);
    $('#dialog').css('left', winW / 2 - $('#dialog').width() / 2);
    $('#dialog').fadeIn(1000);
}
function ShowNewsLetterSignUp() {
    $('#dialog').css('height', '400px');
    $('#dialog').css('width', '550px');
    TotalSportsWebsite.Services.TS.OpenNewsletterSignupWindow(LoadAjaxContent);
}
function ShowNewsLetterSignUpPrePop() {
    TotalSportsWebsite.Services.TS.OpenNewsletterSignupWindowWithOptions($("#ContentPlaceHolder1_txtName").val(), $("#ContentPlaceHolder1_txtEmail").val(), $("#ContentPlaceHolder1_txtMobile").val(), LoadAjaxContent);
}
function LoadAjaxContent(result) {
    if (result != "") {
        $("#AjaxContent").html(result);
        OpenWindow();
        _typeface_js.initialize();
    }
}
function ShowLogin() {
    $('#dialog').css('height', '300px');
    TotalSportsWebsite.Services.TS.OpenLoginWindow(LoadAjaxContent);
}
function LoginUser() {
    TotalSportsWebsite.Services.TS.LoginUser($("#txtUserName").val(), $("#txtPassword").val(), $("#chkRememberMe").attr('checked'), ProcessLoginResult, ProcessLoginResult, ProcessLoginResult);
}
function ProcessLoginResult(result) {
    var args = result.toString().split('|');
    if (args[0] == "true") {
        $('.window').fadeOut(1000);
        $('#mask').fadeOut(1000);
        UpdateWishListItemCount();
    }
    else {
        alert(result);
    }
}
function OpenRegisterWindow() {
    $('#dialog').css('height', '500px');
    $('#dialog').css('width', '580px');
    TotalSportsWebsite.Services.TS.OpenRegisterWindow(LoadAjaxContent);
}
function RegisterUser() {
    if ($("#txtName").val() == "" || $("#txtSurname").val() == "" || $("#txtEmail").val() == "" || $("#ddlProvince").val() == "" || $("#ddlGender").val() == "") {
        alert("Please complete all the fields marked with a *");
    }
    else {
        //1. Check that they passwords match
        if ($("#txtPassword").val() == $("#txtPasswordConfirm").val()) {
            //2. Check that the email address is valid
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($("#txtEmail").val())) {
                //3. Check if the user wants to be subscribed to any mailers
                var subscriptions = "";
                if ($("#chkTrendupDates").attr('checked')) {
                    subscriptions += $("#chkTrendupDates").val() + ", ";
                }
                if (subscriptions != "") {
                    //Subscribe User.
                    TotalSportsWebsite.Services.TS.NewsLetterSignup($("#txtName").val(), $("#txtSurname").val(), $("#txtEmail").val(), $("#txtmsisdn").val(), $("#ddlGender").val(), $("#ddlProvince").val(), $("txtAccount").val(), "", NewsletterSignUp_Callback);
                }
                //4. Register User
                TotalSportsWebsite.Services.TS.RegisterUser($("#txtName").val(), $("#txtSurname").val(), $("#txtEmail").val(), $("#txtmsisdn").val(), $("#txtPassword").val(), RegisterCallBack)
            }
            else {
                alert("Invalid E-mail Address! Please re-enter.");
            }
        }
        else {
            alert("Password do not match");
        }
    }
}
function SubscribeNow() {
    //Check Required Fields
    if ($("#txtName").val() == "" || $("#txtSurname").val() == "" || $("#txtEmail").val() == "" || $("#ddlProvince").val() == "" || $("#ddlGender").val() == "") {
        alert("Please complete all the fields marked with a *");
    }
    else {
        //Ensure the email address is valid
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($("#txtEmail").val())) {
            TotalSportsWebsite.Services.TS.NewsLetterSignup($("#txtName").val(), $("#txtSurname").val(), $("#txtEmail").val(), $("#txtmsisdn").val(), $("#ddlGender").val(), $("#ddlProvince").val(), $("#txtAccount").val(), "", NewsletterSignUp_Callback);
        }
        else {
            alert("Invalid E-mail Address! Please re-enter.");
        }
    }
}
function NewsletterSignUp_Callback(result) {
    var arg = result.split('|');
    if (arg[0] == "false") {
        alert(arg[1]);
    }
    else {
        alert(arg[1]);
        $('.window').fadeOut(1000);
        $('#mask').fadeOut(1000);
    }
    
}

function RegisterCallBack(result) {
    var args = result.toString().split('|');
    if (args[0] == "true") {
        $("#ctl00_lnkLogin").hide();
        $("#ctl00_lnkLogout").show();
        $('.window').fadeOut(1000);
        $('#mask').fadeOut(1000);
        UpdateWishListItemCount();
    }
    else {
        alert(args[1]);
    }
}

function AddToWishlist(productid) {
    TotalSportsWebsite.Services.TS.AddToWishList(productid, WishListCallBack);
}
function WishListCallBack(result) {
    var args = result.toString().split('|');
    UpdateWishListItemCount();
    if (args[1] == "login") {
        ShowLogin();
    }
    else {
        alert(args[1]);
    }
}

function UpdateWishListItemCount() {
    TotalSportsWebsite.Services.TS.UpdateWishListItemCount(UpdateWishListItemCount_CallBack);
}
function UpdateWishListItemCount_CallBack(result) {
    if (result != "") {
        $("#wishlistcountwrapper").html(result);
    }
}

function DrawWishList() {
    $('#dialog').css('height', '400px');
    $('#dialog').css('width', '550px');
    TotalSportsWebsite.Services.TS.DrawWishlist(DrawWishList_CallBack);
}
function DrawWishList_CallBack(result) {
    if (result != "") {
        $("#AjaxContent").html(result);
        OpenWindow();
       $('.wishlistwrapper').jScrollPane();
    }
}
function RemoveWishListItem(productid, objectid) {
    $("#" + objectid).remove();
    TotalSportsWebsite.Services.TS.DeleteWishlistItem(productid, RemoveWishListItem_Callback);
}
function RemoveWishListItem_Callback(result) {
    UpdateWishListItemCount();
    if (result == "false") {
        $('.window').fadeOut(1000);
        $('#mask').fadeOut(1000);
    }
}
function OpenSendWishList() {
    $('#dialog').css('height', '400px');
    $('#dialog').css('width', '600px');
    TotalSportsWebsite.Services.TS.OpenSendWishList(LoadAjaxContent);
}

function SendWishlistItems() {
    TotalSportsWebsite.Services.TS.SendWishlistItems($("#txtWLSenderName").val(), $("#txtWLSenderEmail").val(), $("#txtWLFriend1").val(), $("#txtWLFriend1Email").val(), $("#txtWLFriend2").val(), $("#txtWLFriend2Email").val(), $("#txtWLFriend3").val(), $("#txtWLFriend3Email").val(), $("#txtWLText").val(), LoadAjaxContent);
}
function SendWishlistItems_CallBack(result) {
    alert(result);
}


function GetWallpaper(downloadid) {
    $('#dialog').css('height', '400px');
    $('#dialog').css('width', '550px');
    TotalSportsWebsite.Services.TS.OpenWallpaperDownloadWindow(downloadid, LoadAjaxContent);
}

function OpenWishList(wishlistid) {
   var newWin =  window.open("/PrintWishlist.aspx?id=" + wishlistid, "_blank", "width=800,height=600,menubar=false,resizable=no", false);
    if (!newWin || newWin.closed || typeof newWin.closed == 'undefined') {
        alert("Print Wishlist Page has been blocked\n please allow through your popup blocker");
    }
}

function OpenWishList_CallBack(result) {
    if (result != "") {
        $("#AjaxContent").html(result);
        OpenWindow();
        $('.viewlistwrapper').jScrollPane();
    }
}
function SearchRedirect() {
    if ($("#txtSearch").val() != "") {
        document.location.href = "/SearchResults.aspx?s=" + $("#txtSearch").val();
    }
    else {
        alert("Please enter search criteria");
    }
}

function TotalSportsAlert(message) {
    TotalSportsWebsite.Services.TS.TSAlert(message, LoadAjaxContent);
}

