﻿// === PARAMO PRODUCT FUNCTIONS === //

// Eonic Ltd 2009 - Webesite Design and Development
var dpGarmentQuantity = 1;


var doAjax = 0;

$(document).ready(function() {

    // Make all developer links open in a new window

    $('#developerLink a').attr('target', '_blank');

    /*========================== PARAMO PARTNERS FUNCTIONS =====================================*/


    $('.modalPopUp').click(function(e) {
        e.preventDefault();

        var skuid = new String;
        skuid = $(this).parents('.list').find('.qtybox').attr('id');
        skuid = skuid.substr(4);
        var href = $(this).attr('href');

        var connector = new String;

        var features = 'toolbar=0,scrollbars=1,resize=1,location=0,menubar=0,width=800,height=560';
        var winName = 'GarmentDetail';
        if ($('#adminHeader').length > 0) {
            connector = '&';
        }
        else {
            connector = '?';
        }

        randomVar = Math.floor(Math.random() * 150);
        /*Test string to bring back xml*/
        /*href = href + connector + 'modalDetail' + '=&forceNewRequest=' + randomVar + '&skuid=' + skuid + '&xml=x';*/
        href = href + connector + 'modalDetail' + '=&forceNewRequest=' + randomVar + '&skuid=' + skuid;
        OpenWindow(href, winName, features);

    })

    // Quick fix to force all links in popup window to open in a new window
    $('#popUpMainTable a').attr('target', '_blank');


    $('input.searchbox').click(function() { this.value = '' });

    $('.imgswp').livequery('mouseover', function(event) {
        /*Note Id is in the following format 'skuid + '_' + position + '_' + color'*/

        var Id = new String;
        var garmentId = new String;
        var idArray = new Array();
        Id = $(this).attr('id');
        idArray = Id.split('_');
        garmentId = $(this).parents('.productContainer').attr('id');
        colorName = '#' + garmentId + ' .imagePlaceHolder .' + idArray[2];
        colorName = '#' + garmentId + ' .' + idArray[2];
        var colourImageShow = '#' + garmentId + ' .colourImageShow';
        $(colourImageShow).removeClass('colourImageShow').addClass('colourImageHide');
        $(colorName).addClass('colourImageShow');
        $(colorName).removeClass('colourImageHide');
    });

    $('a.stockIcon').livequery('click', function() {
        /*Note Id is in the following format 'skuid + '_' + position + '_' + color'*/
        var Id = new String;
        var idArray = new Array();
        Id = $(this).attr('id');
        idArray = Id.split('_');
        $('.qtybox').attr('id', 'qty_' + idArray[0]).attr('name', 'qty_' + idArray[0]);
        if ($(this).hasClass('zeroStock')) {
            return;
        }
        else {
            $(this).parents('form').submit();
        }
    });

    $('.garmentoptions').click(function(e) {
        /*Prevent browser navigating to link - COZ THAT'S WHY WE IZ AJAXIN!*/
        e.preventDefault();
        /*Set up variables*/
        var productId = new String;
        var queryString = new String;
        var parentContainer = new String;
        var hrefQuestionMark = new Number;
        var pageIdPosition = new Number;
        var imagePlaceHolder = new String;
        var connector = new String;
        if ($(this).is('.admin')) {
            connector = '&';
        }
        else {
            connector = '?';
        }

        /*console.log('connector ' + connector);*/


        productId = $(this).attr('id');
        productId = productId.substr(3);
        href = $(this).attr('href');

        parentContainer = '#' + $(this).parents('div.productContainer').attr('id');
        //create selector
        currentAvailabilityTable = '#' + $(this).parents('div.productContainer').attr('id') + ' .productAvailability table';

        imagePlaceHolder = '#imagePlaceHolder_' + productId;

        if ($(this).hasClass('hideoptions')) {

            //delete current options table
            $(currentAvailabilityTable).fadeOut('normal');

            $(imagePlaceHolder).children('div').slideUp('normal');

            $(this).removeClass('hideoptions').text('Show Size & Colours');
            $(parentContainer + ' .column2 .lightbox:first img').slideDown('slow');
        }
        else {

            //Hide thumbnail image
            $(parentContainer + ' .column2 .lightbox:first img').slideUp('normal');


            if ($(currentAvailabilityTable).is(':hidden')) {
                $(currentAvailabilityTable).fadeIn('slow');
                $(imagePlaceHolder).children('div').slideDown('normal');
            } else {
                getavailabilityTable(productId, 'getAvailabilityTable', parentContainer, href, connector);
                getProductImages(productId, 'getProductImages', href, connector);
            }
            $(this).addClass('hideoptions').text('Hide Size & Colours');
        }
    });


    /*=====================================================================================*/




    /*========================== PARAMO PARTNERS AJAX CALLS =====================================*/


    function getavailabilityTable(productId, queryString1, parentContainer, href, connector) {

        ajaxSelector = parentContainer + ' .productAvailability';
        randomVar = Math.floor(Math.random() * 150);
        href = href + connector + queryString1 + '=&forceNewRequest=' + randomVar
        //alert(href);

        $(ajaxSelector).html('<div class="loadingLine"><img src="/images/layout/ajax-loader.gif" width="32" height="32" alt="Loading Ajax Content" title="Loading Ajax Content"/></div>');
        $(ajaxSelector).load(href);
    }

    function fadeInOptions(ajaxSelector) {
        $(ajaxSelector).show('slow');
    }


    function getProductImages(productId, queryString2, href, connector) {
        imagePlaceHolder = '#imagePlaceHolder_' + productId
        randomVar = Math.floor(Math.random() * 150);
        href = href + connector + queryString2 + '=&forceNewRequest=' + randomVar

        $(imagePlaceHolder).html('<div class="loadingLine"><img src="/images/layout/ajax-loader.gif" width="32" height="32" alt="Loading Ajax Content" title="Loading Ajax Content"/></div>');
        
        //Load new content and then reinistialise lightbox on the call back function.
        $(imagePlaceHolder).load(href, function() {
        $('a.lightbox').lightbox();
        });
    }

    $('a.lightbox').lightbox();


    $(function() {
        $("#viewer").imageScroller({
            next: "btn1",
            prev: "btn2",
            frame: "viewerFrame",
            width: 100,
            child: "a",
            auto: false
        });
    });

    //Swatch click function
    $(".swatchPanel a").click(function() {
        var chosenColor = $(this).attr('id');
        chosenColor = chosenColor.substring(12);
        var chosenImage = '#ColourImage_' + chosenColor;

        //Remove activeSwatch class from all swatches
        $(".swatchPanel a").each(function() { $(this).removeClass('activeSwatch') });
        $(".swatchPanel p").each(function() { $(this).removeClass('activeSwatch') });
        //Add activeSwatch class to clicked swatch
        $(this).addClass('activeSwatch');
        //Add activeSwatch class to swatch colour name text.
        $(this).next().addClass('activeSwatch');


        //Check to see if there are is a corresponding garment image available before swapping garment image
        if ($(chosenImage).length > 0) {
            $(".colourImageShow").each(function() {
                $(this).removeClass('colourImageShow')
                $(this).addClass('colourImageHide')
            });
            $(chosenImage).addClass('colourImageShow');
        }


        //Build options list ID	
        var swatchOptionsId = '#options_' + chosenColor;

        // Hide all open dropdown lists
        $('.swatchOptionsOpen').each(function() {
            $(this).removeClass('swatchOptionsOpen');
            $(this).addClass('swatchOptionsClosed');
        });

        // Display corresponding dropdown list to clicked swatch
        $(swatchOptionsId).removeClass('swatchOptionsClosed');
        $(swatchOptionsId).addClass('swatchOptionsOpen');

        // reset First price to show as default
        var swatchOptionsPrices = swatchOptionsId + ' .showGarmentPrice';
        var swatchOptionsFirstPrice = swatchOptionsId + ' span:first';

        $(swatchOptionsPrices).each(function() {
            $(this).addClass('hideGarmentPrice');
            $(this).removeClass('showGarmentPrice');
        });

        $(swatchOptionsFirstPrice).addClass('showGarmentPrice');
        $(swatchOptionsFirstPrice).removeClass('hideGarmentPrice');

        $(swatchOptionsFirstPrice).addClass('showGarmentPrice');


        // reset dropdownlists
        $('select').each(function() {
            $(this)[0].selectedIndex = 0;
        });

        // Stock check on first selected SKU to enable/disable cart button Cart Button
        var firstStockValue = $("#" + swatchOptionsId + " select :selected").attr('class').substring(6);
        if (firstStockValue == 0) {
            document.getElementById('cartAdd').setAttribute('disabled', 'disabled');
            $('#cartAdd').addClass('disabled');
        } else {
            document.getElementById('cartAdd').removeAttribute('disabled', 'disabled');
            $('#cartAdd').removeClass('disabled');
        }

        // GET ID OF NEW SELECTED COLOUR - Change Qty Buttons
        var newQuantityValue = 'qty_' + $('.swatchOptionsOpen select').val();
        $('#cartDetailLeft .qtybox').attr("id", newQuantityValue);
        $('#cartDetailLeft .qtybox').attr("name", newQuantityValue);

        // Disable page refresh from click on 'A' link
        return false;
    });

    $('.ajaxoptions').each(function() {
        var randomVar = Math.floor(Math.random() * 150);
        var href = $(this).attr('rel');
        href = href + '?getProductOptions' + '=&forceNewRequest=' + randomVar
        var ajaxSelector = $(this).attr('id');
        //ajaxSelector = ajaxSelector.split('_')[1];
        $(this).html('<img src="/images/layout/kit-ajax-loader.gif" width="43" height="11" alt="Loading Ajax Content" title="Loading Ajax Content"/>');
        /**/

        $(this).load(href, function() {
            mySelector = '#' + ajaxSelector + ' select option';
            $(mySelector).each(function(index) {
                if (index == 1) {
                    var garmentId = $(this).val();
                    garmentId = 'qty_' + garmentId;
                    $(this).parents('div').next('.columnB').find('label').attr('for', garmentId);
                    $(this).parents('div').next('.columnB').find('.qtybox').attr('id', garmentId).attr('name', garmentId);
                }
            });

        });
    })


    $('.columnA select').livequery('change', function() {
        var garmentId = $(this).val();
        garmentId = 'qty_' + garmentId;
        priceBriefTableId = '#skuPrice_' + $(this).val();
        $(this).parents('div').next('.columnB').find('label').attr('for', garmentId);
        $(this).parents('div').next('.columnB').find('.qtybox').attr('id', garmentId).attr('name', garmentId);
        $(this).parent('div').find('div').hide();
        $(priceBriefTableId).show();
    });

});



function showForm(formId,oButton){
    document.getElementById(formId).className = 'reviewFormShow';
    oButton.style.display = 'none';
}


/* ON CHANGE OF DISCOUNT PRODUCT SKU SELECTION */
function dpUpdateSelectedSKU(skuSelect,garmentId){
	var selectedSKUId = $('#' + skuSelect).val();
	var newSelectedQTY = 'qty_' + selectedSKUId;
	var garmentQTY = '.gqty_' + garmentId;
	if (selectedSKUId == '') {
		$(garmentQTY).removeAttr('value');
		$(garmentQTY).removeAttr('id');
		$(garmentQTY).removeAttr('name');
	} else if ($('#discountProducts' + garmentId).is(':checked')){
		$(garmentQTY).attr('id', newSelectedQTY);
		$(garmentQTY).attr('name', newSelectedQTY);
		$(garmentQTY).attr('value', dpGarmentQuantity);
	}
}

/* ON CART FORM SUBMISSION - CHECK SELECTED FREE ITEM */
function checkDiscountSelection(formId){
	var bCartValid = false;
	var dgSelectedRadioId = $('.dgSelector:checked').val();
	var nParentQuantity = $('#cartDetailLeft .qtybox').val();
	
	if ($('.gqty_' + dgSelectedRadioId).val() > 0){
		$('.gqty_' + dgSelectedRadioId).val(nParentQuantity);
		bCartValid = true;
	} 	
	if (!bCartValid){
		alert('Please choose your accompaning product');
		$('#dp-SKUVariants' + dgSelectedRadioId).focus();
	}
	return bCartValid;
}

/* Change Cart Values */
function changeCartValues(selectedGarmentId){
	$('.dgValue').each(function(){
		$(this).removeAttr('value');
		$(this).removeAttr('id');
		$(this).removeAttr('name');
	});
	var nIsValue = $('#dp-SKUVariants' + selectedGarmentId).val();
	if (nIsValue > 0){
		var garmentQTY = '.gqty_' + selectedGarmentId;
		var newSelectedQTY = 'qty_' + nIsValue;
		$(garmentQTY).attr('id', newSelectedQTY);
		$(garmentQTY).attr('name', newSelectedQTY);
		$(garmentQTY).attr('value', dpGarmentQuantity);
    }
}


function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

$(document).ready(function() {


    equalHeight($(".Site #product_brief_2_col_listing .garment"));


});