﻿/****************************************************************************************	
[GLOBAL JQUERY FUNCTIONS]
*****************************************************************************************/
jQuery.noConflict();

jQuery(document).ready(function ($) {


    /*Search Start*/

    jQuery("#btnHeadSearch").click(function () {

        var encoded = encodeURIComponent(jQuery("#txtHeadSearch").val());

        var filters = jQuery("#header .search input[type='hidden']");

        if (filters != null && filters.length > 0) {

            alert('search invoked' + jQuery(filters[1]).val());

            window.location = "http://www.microsoft.com";
            location.replace("http://www.microsoft.com");

        }
    });

    /*Search End*/



    $(".image-select").click(function () {
        
        $("#image-bank input[type='checkbox']").attr('checked', true);
        $("#image-bank input[type='checkbox']").parent().addClass('checked');

        RegenerateHref();
        
        ShowHideSubmitButton();
    });

    $(".image-deselect").click(function () {
        
        $("#image-bank input[type='checkbox']").attr('checked', false);
        $("#image-bank input[type='checkbox']").parent().removeClass('checked');

        RegenerateHref();

        ShowHideSubmitButton();
    });

    $("#image-bank input[type='checkbox']").click(function () {
        
        RegenerateHref();

        ShowHideSubmitButton();
    });

    function RegenerateHref() {

        var imageIds = "";
        $("#image-bank input:checked").each(
          function () {            

              if (imageIds != "") {
                  imageIds += "|" + $(this).attr('id');
              }
              else
                  imageIds = $(this).attr('id');
          }
        );

        // Have to break out the modal specific keys and then add our querystring to the front, i.e. adding to the end
        // doesn't work.
        var href = $('#submitImageRequestButton').attr('href').toString();
        var url = href.substring(0, href.indexOf('?'));
        var modalKeys = href.substring(href.indexOf('TB_'));
        var myQueryString = href.substring(href.indexOf('?'), href.indexOf('TB_') - 1);
        var newQueryString = insertParam(myQueryString, 'imageids', imageIds);

        $('#submitImageRequestButton').attr('href', url + "?" + newQueryString + ((newQueryString != "") ? "&" : "") + modalKeys);

    }

    function ShowHideSubmitButton() {
        if ($("#image-bank input:checked").length > 0)
            $('#submitImageRequestButton').show();
        else
            $('#submitImageRequestButton').hide();
    }

    function insertParam(querystring, key, value) {
        //key = escape(key); 
        //value = escape(value);

        var kvp = querystring.substr(1).split('&');

        if (kvp == "") {
            querystring = key + "=" + value;
        }
        else {
            var i = kvp.length; var x; while (i--) {
                x = kvp[i].split('=');

                if (x[0] == key) {
                    x[1] = value;
                    kvp[i] = x.join('=');
                    break;
                }
            }

            if (i < 0) { kvp[kvp.length] = [key, value].join('='); }

            //this will reload the page, it's likely better to store this until finished
            querystring = kvp.join('&');
        }
        return querystring;
    }

});
