﻿$(document).ready(function () {

    // Add pdf icons to pdf links
    $(".attachments a").addClass("download"); //default icon, might be overridden later    
    $(".attachments a[href$='.pdf']").addClass("pdf");
    $(".attachments a[href$='.mp3'], .attachments a[href$='.wav']").addClass("audio");
    $(".attachments a[href$='.doc'], .attachments a[href$='.rtf'], .attachments a[href$='.docx']").addClass("word");
    $(".attachments a[href$='.xls'], .attachments a[href$='.xlsx'], .attachments a[href$='.csv']").addClass("excel");
    $(".attachments a[href$='.jpg'], .attachments a[href$='.png'], .attachments a[href$='.gif'], .attachments a[href$='.tif'], .attachments a[href$='.tiff'], .attachments a[href$='.bmp']").addClass("picture");
    $(".attachments a[href$='.avi'], .attachments a[href$='.flv'], .attachments a[href$='.wmv'], .attachments a[href$='.mov'], .attachments a[href$='.divx'], .attachments a[href$='.mpeg']").addClass("video");
    $(".attachments a[href$='.zip'], .attachments a[href$='.rar']").addClass("zip");

    document.createElement("header");
    document.createElement("nav");
    document.createElement("section");
    document.createElement("aside");
    document.createElement("article");
    document.createElement("footer");
    document.createElement("figure");
    document.createElement("figcaption");

    // Search field
    $(".searchfield").keyup(function () {
        if (!$(".searchfieldBox").hasClass("searchfieldBoxSelected")) $(".searchfieldBox").focus().addClass("searchfieldSelected", "");
    });

    $(".searchfield").focus(function () {
        if (!$(".searchfieldBox").hasClass("searchfieldBoxSelected")) {
            $(".searchfieldBox").switchClass("", "searchfieldBoxSelected");  //switchClass
        }
    });

    $(".searchfield").blur(function () {
        if ($(".searchfieldBox").hasClass("searchfieldBoxSelected")) {
            $(".searchfieldBox").switchClass("searchfieldBoxSelected", "");
        }
    });



    /*
    // Add email icons to email links
    $("a[href^='mailto:']").addClass("email");

    //Add external link icon to external links - 
    $('a').filter(function() {
    //Compare the anchor tag's host name with location's host name
    return this.hostname && this.hostname !== location.hostname;
    }).addClass("external");
    */

    //You might also want to set the _target attribute to blank
    /*
    $('a').filter(function() {
    //Compare the anchor tag's host name with location's host name
    return this.hostname && this.hostname !== location.hostname;
    }).addClass("external").attr("target", "_blank");
    */
});



//// Product: Add to Cart function
//$(".add2cart").click(function () {
//    var timeout;
//    clearTimeout(timeout);

//    killLoaders();
//    $(".autohide").remove();

//    var q = parseInt($(".quantity :selected").val()); // Selected quantity
//    var qText = $(".quantity :selected").text(); // Quantity text (i.e. 1 stk)
//    var vID = parseInt($(".variations :selected").val()); // Variation ID
//    var vText = $(".variations :selected").text(); // Variation title

//    var button = $(this).fadeTo("fast", 0.5).attr("disabled", true);
//    button.parent().after("<span class='blueloader loading'>Laster...</span>"); // Append loader

//    if (isNaN(q) || q <= 0 || isNaN(vID) || vID <= 0) {
//        console.log("# add2cart: Invalid quantity or variationID (q: " + q + ", vID: " + vID + "). Cancelling...");
//        $(".ui-state-error").remove();
//        button.fadeTo("fast", 1).attr("disabled", false).parent().after("<span class='ui-state-error ui-corner-all'>Ugyldig størrelse eller variasjon...</span>");

//        var msg = button.parent().parent().find(".ui-state-error");
//        if (msg != null) msg.effect("pulsate");
//        killLoaders();
//        return false;
//    }

//    $.ajax({
//        type: "POST",
//        url: "/modules/ecommerce35/ECommerceAjaxWebService.asmx/Add2Cart",
//        data: "{'quantity':" + q + ",'variationID':" + vID + "}",
//        contentType: "application/json; charset=utf-8",
//        dataType: "json",
//        success: function (r) {
//            console.log(r);
//            if (r.d != "ERR") {
//                killLoaders();
//                var confirmText = qText + " " + $(".productTitle").text() + " " + vText + " lagt til din <a href='" + $(".toCartButton").attr("href") + "' class=\"focusMe\">handlevogn</a>.";
//                button.fadeTo("fast", 1).attr("disabled", false).parent().after("<span class='ui-state-highlight ui-corner-all autohide'>" + confirmText + "</span>");
//                $(".cartQ").html(parseInt($(".cartQ:first").text()) + q); // Update cart quantity
//                $("#cartS").html(r.d);
//                timeout = setTimeout(function () { $(".autohide").fadeOut("slow", function () { $(this).remove(); }); }, 10000);

//                if ($(".focusMe").length > 0) $(".focusMe").focus();
//            }
//        }
//    });

//    return false;
//});

//$(document).ready(function () {
//    if (!($.browser.msie && $.browser.version <= 6)) //Don't activate for IE6
//    {
//        jQuery("input.livesearchbox").liveSearch({ ajaxURL: '/api/ProductSearch.asmx/search' });
//    } else {
//        $("#sokboks_masterpage").hide();
//    }
//});

function fillTextBox(place) {
    switch (place) {
        case 'name':
            $(".delName").val($(".newName").val());
            if ($(".invoName").length) {
                $(".invoName").val($(".newName").val());
            }
            break;
        case 'surname':
            $(".delSurname").val($(".newSurname").val());
            if ($(".invoSurname").length) {
                $(".invoSurname").val($(".newSurname").val());
            }
            break;
        default:
            break;
    }
}
