﻿
$(document).ready(function () {


    if ($('#gallery a').lightBox != undefined) {
        //init light box
        $('#gallery a').lightBox();
    }

    //assign li hover class for IE6
    $("li").hover(
          function () {
              $(this).addClass('hover');
          },
          function () {
              $(this).removeClass('hover');
          }
        );

    $('.data-link').live('click', function () {
        document.location = this.getAttribute('data-url')
        return false;
    });

    $('.data-link-newtab').live('click', function () {
        window.open(this.getAttribute('data-url'));
        return false;
    });

    /* search controls */
    $('.quick-search, .search').each(function () {

        var searchButton = $(this);
        var searchArea = searchButton.parents('.search-area');

        searchButton.click(function () {
            document.location = $(this).attr('data-url') + '?' + serializeForm(searchArea);
        });

    });

    $('.search-area').each(function () {

        var searchButton = $(this).find('.quick-search, .search');

        //set up search so 'enter' submits
        $(this).find(':input').live('keypress', function (e) {

            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {

                searchButton.click();
                return false;
            }
        });
    });

    $('.clear-form').click(function () { 
       
        $('#product-search :input').each(function() {
            var type = this.type;
            var tag = this.tagName.toLowerCase(); // normalize case
            if (type == 'text' || type == 'password' || tag == 'textarea')
                this.value = "";
            else if (type == 'checkbox' || type == 'radio')
                this.checked = false;
            else if (tag == 'select')
                this.selectedIndex = -1;
        });
    });


    /* product page */
    $('.product-thumbnails .product-thumbnail img').click(function () {

        var src = $(this).attr('src').replace('_thumb.', '.');

        $('.product-image img').attr('src', src);
    });

    function serializeForm(elements) {
        //cant use serialize as we need to comma separate multiple values
        //assume 1 item per criteria
        var queryString = '';

        elements.find('.criteria').each(function () {

            var items;
            var name;

            //find inputs
            var inputs = $(this).find('input:text, input:checkbox:checked');

            if (inputs.length > 0) {
                items = '';
                name = inputs.attr('name');

                inputs.each(function () {

                    if (items != '') {
                        items += ',';
                    }

                    items += encodeURIComponent($(this).val());
                });


                if (queryString != '') {
                    queryString += '&';
                }

                queryString += name + '=' + items;
            }
        });

        return queryString;
    }

});
