﻿function ScrollTop() {
    window.scrollTo(0, 0);
    FitLoader()
}

function FitLoader() {
    if ($("#update-right-content-container").length > 0 && $("#loading").length > 0) {
        var pos = $("#update-right-content-container").offset();
        $("#loading").css({ "left": (pos.left) + "px" });
        $("#loading").width($("#update-right-content-container").width());
    }
}

function EndRequestHandler(ajaxContext) {
    InitSendFeedback(ajaxContext);

    if ($("#ParentCategoryID").length > 0) {
        $("#ParentCategoryID").change(function () {

            var strCategoryId = "";
            $("#ParentCategoryID option:selected").each(function () {
                strCategoryId = $(this)[0].value;
            });

            var url = "/GetChildCategories/" + strCategoryId;
            $("#ChildCategoryID").empty();
            $.getJSON(url, null, function (data) {
                $.each(data, function (optionData) {
                    $("#ChildCategoryID").append($("<option></option>").val(this['Value']).html(this['Text']));
                });
            });
        });
    }
    setTimeout("Cufon.refresh()", 950);
    if ($("#tabs").length > 0) {
        $("#tabs").tabs({ fx: { width: 'toggle', duration: '200'} });
    }
    InitProductButtonPanels();
}

function InitRecentlyViewedProducts() {
    $('.panel-button').each(function () {
        $(this).qtip(
      {
          content: {
              url: $(this).attr('rel'),
              method: 'get'
          },
          position: {
            corner: 'bottomLeft',
              adjust: {
              x:1,
              y:-25,
                  screen: true // Keep the tooltip on-screen at all times
              }
          },
          show: {
              when: 'click',
              fixed: true,
              solo: true // Only show one tooltip at a time
          },
          hide: 'unfocus',
          style: {
              tip: false, 
              border: {
                  width: 1,
                  radius: 4,
                  color: '#9a9a9a'
              },
              name: 'light', // Use the default light style
              width: 220 // Set the tooltip width
          }
      })
    });
}

function InitAdReviewDialog() {
    $('a[name=addReview]').each(function () {
        $(this).qtip(
   {
       content: {

           url: $(this).attr('rel'),
           method: 'get'
       },
       position: {
           target: $(document.body), // Position it via the document body...
           corner: 'center' // ...at the center of the viewport
       },
       show: {
           when: 'click', // Show it on click
           solo: true // And hide all other tooltips
       },
       hide: 'unfocus',
       style: {
           width: 480,
           padding: '10px',
           border: {
               width: 1,
               radius: 4,
               color: '#646F6F'
           },
           name: 'light'
       },
       api: {
           beforeShow: function () {
               // Fade in the modal "blanket" using the defined show speed
               $('#qtip-blanket').fadeIn(this.options.show.effect.length);
               setTimeout("Cufon.refresh()", 950);
               if ($("input[name=Rating]").length > 0) {
                   $('input[name=Rating]').rating();
               }
           },
           onShow: function () {
               showRecaptcha('recap');
           },
           beforeHide: function () {
               // Fade out the modal "blanket" using the defined hide speed
               $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
           }
       }
   });
    });
}



function InitScreenshotDialog() {
    $('#smallScreenshot').each(function () {
        $(this).qtip(
   {
       content: {
           text: '<img id="#imgBigScreenshoot" style="cursor:pointer;" onclick="$(this).parents(\'.qtip\').qtip(\'hide\');" src="' + $(this).attr('rel') + '" />'
       },
       position: {
           target: $(document.body), // Position it via the document body...
           corner: 'center' // ...at the center of the viewport
       },
       show: {
           when: 'click', // Show it on click
           solo: true // And hide all other tooltips
       },
       hide: 'unfocus',
       style: {
            width: 400,
           padding: '10px',
           border: {
               width: 1,
               radius: 4,
               color: '#646F6F'
           },
           name: 'light'
       },
       api: {
           beforeShow: function () {
               // Fade in the modal "blanket" using the defined show speed
               $('#qtip-blanket').fadeIn(this.options.show.effect.length);
           },
           beforeHide: function () {
               // Fade out the modal "blanket" using the defined hide speed
               $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
           },
           onContentUpdate: function () { this.updateWidth(); },
           onContentLoad: function () { this.updateWidth(); },
           beforeContentLoad: function () { this.updateWidth(); }

       }
   });
    });
}


$(document).ready(function () {
    InitAdReviewDialog();
    InitScreenshotDialog();
    InitRecentlyViewedProducts();

    // Create the modal backdrop on document load so all modal tooltips can use it
    $('<div id="qtip-blanket">')
      .css({
          position: 'absolute',
          top: 0, // Use document scrollTop so it's on-screen even if the window is scrolled
          left: 0,
          height: $(document).height(), // Span the full document height...
          width: '100%', // ...and full width

          opacity: 0.7, // Make it slightly transparent
          backgroundColor: 'black',
          zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
      })
      .appendTo(document.body) // Append to the document body
      .hide(); // Hide it initially

    Cufon.replace('h1, h2, h3, h4', { fontFamily: 'Swis721 Cn BT' });
    Cufon.replace('#logo', { fontFamily: 'Swis721 Cn BT' });
    Cufon.replace('h1 b, h2 b, h3 b, h4 b', { fontFamily: 'Swis721 Cn BT Bold', color: '-linear-gradient(#f47b00, #db2800)' });
    Cufon.replace('.top-blue', { fontFamily: 'Swis721 Cn BT',
        hover: {
            fontFamily: 'Swis721 Cn BT Bold', color: '-linear-gradient(#f47b00, #db2800)'
        }
    });
    if ($("#tabs").length > 0) {
        $("#tabs").tabs({ fx: { width: 'toggle', duration: '200'} });
    }
    //        Cufon.now();
    if ($("#update-right-content-container").length > 0) {
        $("#update-right-content-container").resize(
                function () {
                    FitLoader();
                }
            );
    }

    InitProductButtonPanels();

    FitLoader();
});

function InitProductButtonPanels() {
    $("td.product-cell").mouseenter(function () {
        $(this).addClass("product-cell-active");
        $(this).find("div.product-buttons-panel").show();
    });
    $("td.product-cell").mouseleave(function () {
        $(this).removeClass("product-cell-active");
        $(this).find("div.product-buttons-panel").hide();
    });
}

function InitSendFeedback(ajaxContext) {
    if ($("#contactForm").length > 0) {
        //Sys.Mvc.FormContext.OnSuccessEnableClientValidation(ajaxContext);
        try {
            InitValidation();
        }
        catch (e) { }
        $("#contactForm").submit(function (event) {
            eval($(this).attr("onsubmit"));
            event.preventDefault(); 
            return false; 
        });
        $("#SendFeedback").click(function () {
            //var form = document.forms[0];
            var form = $("#contactForm");
            form.submit();
            return false;
        });
    }
}

function InitValidation() {
    Sys.Application.remove_load(arguments.callee);
    Sys.Mvc.FormContext._Application_Load();
}


//----------------recaptcha--------------------
function showRecaptcha(element) {
    var RecaptchaOptions = {
        theme: 'white',
        lang: 'en',
        custom_theme_widget: 'recaptcha_widget'
    };
    Recaptcha.create("6LfJHr4SAAAAAMyAsBChL0JLN3ZSP9zEhz65vuaT", element, RecaptchaOptions);
    RecaptchaStr_en.play_again = "replay";
    RecaptchaStr_en.cant_hear_this = "can't hear";
}

function ValidateRecaptcha() {
    $.ajax({
        type: "POST",
        url: "/Shared/ValidateRecapatcha/",
        data: { challengeValue: Recaptcha.get_challenge(), responseValue: Recaptcha.get_response() }, 
        success: onValidateRecaptchaSuccess,
        error: onValidateRecaptchaFailed
    });
    //GetWebService().ValidateRecaptcha(Recaptcha.get_challenge(), Recaptcha.get_response(), onValidateRecaptchaSuccess, onValidateRecaptchaFailed);
}

function onValidateRecaptchaSuccess(ret) {
    if (ret == true) {
        $("form:last").submit();
    }
    else {
        var ul = $("#validationSummary ul");  

         if (ul.length > 0) {  
             $("#validationSummary").addClass("validation-summary-errors");  
             $("#validationSummary").removeClass("validation-summary-valid");  
             ul.html("");
             ul.append("<li>Incorrect RECAPTCHA words, please try again</li>")  
         }  
        //Reload recaptcha in case it fails 
        Recaptcha.reload();
    }
}

function onValidateRecaptchaFailed(req, status, error) {
    alert('an error has occured');
} 
