﻿/// <reference path="jquery-1.4.1.js" />

function setSlide(index) {
    $('#galleryArea').cycle(index);
  }

$(function () {


    /* +++ Frontpage Gallery area +++*/
    $("#galleryArea").cycle({
		fx: 'fade',
		speed: 'fast',
		pager: $('#galleryNavLink'),
		next: '.galleryNavRight',
		prev: '.galleryNavLeft',
		slideExpr: 'div.slide',
		timeout: 11000
	}).hover(function() {
            $(this).cycle('pause');
      },function(){
            $(this).cycle('resume');
    });
    //    setTimeout(function () {
    //        jQueryStartCycle();
    //    }, 5000);

    /* +++ Reference Gallery area +++*/
    $("#referenceGalleryContainer")
        .cycle({
            fx: 'fade',
            speed: 'fast',
            timeout: 0,
            pager: $('#bulletNavi'),
            next: '#rightNavi',
            prev: '#leftNavi',
            slideExpr: 'div.items'
        });

    /* +++ Productlist area +++*/
    $("#productModuleArea table tr.productModuleRow td span.expand, #productModuleArea table tr.productModuleRow td span.expandChildren").click(function (e) {
        parentRelId = $(this).parent().parent().attr("rel");
        mainTable = $(this).parent().parent().parent();



        if ($(mainTable).find("tr.productRow[rel=" + parentRelId + "]").length > 0) {
            if ($(mainTable).find("tr.productRow[rel=" + parentRelId + "]").hasClass("displayNone")) {
                $(mainTable).find("tr.productRow[rel=" + parentRelId + "]").removeClass("displayNone");
                $(this).removeClass("expand").addClass("expandChildren");
            }
            else {
                //Tester på denne fordi hvis vi ikke er i denne mode
                //kan man fjerne funktionerne
                if (moduleShowMode) {
                    $(mainTable).find("tr.productRow[rel=" + parentRelId + "]").addClass("displayNone");
                    $(this).removeClass("expandChildren").addClass("expand");

                    $(mainTable).find("tr.productRowDescription[rel=" + $(mainTable).find("tr.productRow[rel=" + parentRelId + "]").attr("id") + "]").addClass("displayNone");

                    $(mainTable).find("tr.productRow[rel=" + parentRelId + "]").each(function (index) {
                      //  $(this).find("td>span").removeClass("expandChildren").addClass("expand");
                        $(".productRowDescription[rel=" + $(this).attr("id") + "]").addClass("displayNone");
                    });


                }
            }
        }
    });

    $("#productModuleArea table tr.productRow td span.expand").click(function (e) {

        productRowId = $(this).parent().parent().attr("id");
        mainTable = $(this).parent().parent().parent().parent();

        if ($(mainTable).find("tr.productRowDescription[rel=" + productRowId + "]").length > 0) {
            if ($(mainTable).find("tr.productRowDescription[rel=" + productRowId + "]").hasClass("displayNone")) {
                $(mainTable).find("tr.productRowDescription[rel=" + productRowId + "]").removeClass("displayNone");
                $(this).removeClass("expand").addClass("expandChildren");
            }
            else {
                $(mainTable).find("tr.productRowDescription[rel=" + productRowId + "]").addClass("displayNone");
                $(this).removeClass("expandChildren").addClass("expand");

            }

        }

    });

    var moduleShowMode = true;
    $("#productModuleArea table th #moduleShowMode, #productModuleArea table th #functionShowMode").click(function (e) {
        $("#productModuleArea table tr.productRowDescription").addClass("displayNone");
        $("#productModuleArea table tr.productRow td span.expandChildren").removeClass("expandChildren").addClass("expand");


        if ($(this).attr('id') == 'moduleShowMode') {
            $("#productModuleArea table tr.productModuleRow").not('.isHeader').removeClass("displayNone");
            $("#productModuleArea table tr.productRow").not('.staticView').addClass("displayNone");
            $("#productModuleArea table tr.productRow td span.functionMode").removeClass("functionMode").addClass("expand");
            $(this).addClass("selected");
            $("#productModuleArea table th #functionShowMode").removeClass("selected");
            moduleShowMode = true;

        }
        else {
            $("#productModuleArea table tr.productModuleRow").not('.isHeader').addClass("displayNone");
            $("#productModuleArea table tr.productRow").not('.staticView').removeClass("displayNone");
            $("#productModuleArea table tr.productRow td span.expand").removeClass("expand").addClass("functionMode");
            $(this).addClass("selected");
            $("#productModuleArea table th #moduleShowMode").removeClass("selected");
            $("#productModuleArea table tr.productModuleRow td span.expandChildren").removeClass("expandChildren").addClass("expand");
            moduleShowMode = false;

        }
    });
    $(".productModuleRow, .productRow").hover(
      function () {
          $(this).find("td span.included").css("background-position", "center bottom")
      },
      function () {
          $(this).find("td span.included").css("background-position", "center top")
      }
    );

    $("[id$=_btnSendFormular]").click(function (e) {
        validateSuccess = validate();
        if (validateSuccess == false) {
            $("[id$=_pnlErrorMessage]").removeClass("hide");
            return false;
        }

//        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
//        if (!emailReg.test($("[id$=_txtEmail]").val())) {
//            $("[id$=_pnlErrorMessage]").removeClass("hide");
//            return false;
//        }

        action = $(".formularPanel a.selected span").html();
        acquisitionModules = "";
        packages = "";
        $('input:radio').each(function () {
            if ($(this).is(':checked')) {
                packages += "," + $(this).parent().find("label").html();
            }
        });
        $('input:checkbox').each(function () {
            if ($(this).is(':checked')) {
                acquisitionModules += "," + $(this).parent().find("label").html();
            }
        });

        emailTo = $("[id$=_hidEmail]").val();
        action = $(".formularPanel a.selected span").html();
        firm = $("[id$=_txtFirm]").val();
        address = $("[id$=_txtAddress]").val();
        zipCode = $("[id$=_txtZipCode]").val();
        city = $("[id$=_txtCity]").val();
        contact = $("[id$=_txtContact]").val();
        email = $("[id$=_txtEmail]").val();
        phone = $("[id$=_txtPhone]").val();
        comments = $("[id$=_txtComments]").val();


        var el = $(this);
        var url = "/base/FormService/SendQuery.aspx"
        $.post(url,
        	{ "emailTo": emailTo, "action": action, "firm": firm, "address": address, "zipCode": zipCode, "city": city, "contact": contact, "email": email,
        	    "phone": phone, "comments": comments, "packages": packages, "acquisitionModules": acquisitionModules
        	},
        	function (data) {
        	    if (data == "True") {
        	        $("[id$=_pnlErrorMessage]").addClass("hide");
        	        $("[id$=_pnlSuccessMessage]").removeClass("hide");
        	        $("[id$=_pnlForm]").addClass("hide");
        	    }
        	    else {
        	        $("[id$=_pnlErrorMessage]").removeClass("hide");
        	    }
        	}
        );
        return false;
    });


    $("#referencePageContainer #referenceGalleryContainer #referenceGallery .items .item").hover(
      function () {
          $(this).find(".description").removeClass("hide");
          //$(this).css("cursor", "pointer");
      },
      function () {
          $(this).find(".description").addClass("hide");
          //$(this).css("cursor", "default");
      }
    );

    //http://weblogs.asp.net/karan/archive/2010/10/26/jquery-watermark.aspx
    $(".inputLarge, .inputSmall, .inputMiddle, .inputComment, .txtNewsletter").each(function () {
        $tb = $(this);
        $tb.val(this.title);
    });

    $(".inputLarge, .inputSmall, .inputMiddle, .inputComment, .txtNewsletter").focus(function () {
        $tb = $(this);
        if ($tb.val() == this.title) {
            $tb.val("");
            $tb.removeClass("water");
        }
    });

    $(".inputLarge, .inputSmall, .inputMiddle, .inputComment, .txtNewsletter").blur(function () {
        $tb = $(this);
        if ($.trim($tb.val()) == "") {
            $tb.val(this.title);
            $tb.addClass("water");
        }
    });

});
function postData() {

   
}
function validate() {
    success = true;
    packages = "";
    acquisitionModules = "";

    $(".formularInput input.required").each(function () {
        $tb = $(this);
        if ($tb.val() == $tb.attr("title")) {
            success = false;
        }
    });

    $('input:radio').each(function () {
        if ($(this).is(':checked')) {
            packages += "," + $(this).parent().find("label").html();
        }
    });
    $('input:checkbox').each(function () {
        if ($(this).is(':checked')) {
            acquisitionModules += "," + $(this).parent().find("label").html();
        }
    });

    //alert(acquisitionModules + " # "  + packages );

    if ($(".formularRadioButtons")[0]) {
        //Validering
        if (packages.length == 0) {
            if (acquisitionModules.length == 0) {
                success = false;
            }
        }
    } 


    //alert(acquisitionModules.length + " # " + packages.length);

    return success;
}

function jQueryStartCycle() {
    $("#referenceGalleryContainer")
        .cycle({
            fx: 'fade',
            speed: 'fast',
            timeout: 0,
            pager: $('#bulletNavi'),
            next: '#rightNavi',
            prev: '#leftNavi',
            slideExpr: 'div.items'
        });
}


