﻿$(document).ready(function() {
    var hackIE = $.browser.msie;
    var distance = 10;
    var time = 150;
    var hideDelay = 0;
    var hideDelayTimer = null;
    var beingShown = false;
    var shown = false;

    var info = $('#bubbleInfo .gradient');

    var i = 0;
    $('.winelist li.sep').each(function() {
        i = i + 1;
        if ((i % 4) == 0) {
            $(this).remove();
        }
    });


    $('.OnlineShop li:last').css({ margin: 0 }).prev('li').css({ margin: 0 });

    /*-----------------------------Top Nav ---------------------------*/
    $('#cart_summary').hover(
	function() {
	    $(this).addClass("dd_open");
	    $(this).find('.summary').show();
	},
	function() {
	    $(this).removeClass("dd_open");
	    $(this).find('.summary').hide();
	}

	);
    /*-----------------------------//---------------------------*/


    /*-------------Navigation Manipulation -------------*/
    $('li.top li.selected').parent().parent('li.top').addClass('selected');

    /*-------------------//-------------------*/

    $('.linkWineClubTooltip').mouseover(function() {
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        if (beingShown || shown) { return; }
        else {
            beingShown = true; // reset position of info box
            if (hackIE && !browserLessThanIE8()) { info.css({ top: $(this).y() + 15, left: $(this).x() - 100, display: 'block' }).animate({ top: '-=' + distance + 'px' }, time, 'easein', function() { beingShown = false; shown = true; }); }
            else { info.css({ top: $(this).y() + 30, left: $(this).x() - 100, display: 'block' }).animate({ top: '-=' + distance + 'px', opacity: 1 }, time, 'easein', function() { beingShown = false; shown = true; }); }
        }
        return false;
    }).mouseout(function(e) {
        var space = 10;

        if (hackIE) space = 0;

        if (e.pageY > $(this).y() + space) {

            //move down into the tooltip, the popupbox stays
            hideDelay = 4000;
        } else
        { hideDelay = 0; }



        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer = setTimeout(function() {
            hideDelayTimer = null;
            if (hackIE) { info.animate({ top: '+=' + distance + 'px' }, time, 'easeout', function() { shown = false; info.css('display', 'none'); }); }
            else { info.animate({ top: '+=' + distance + 'px', opacity: 0 }, time, 'easeout', function() { shown = false; info.css('display', 'none'); }); }
        }, hideDelay);
        return false;
    });



    $('body').click(function(event) {
        if (!$(event.target).is('#buttleInfo')) {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function() {
                hideDelayTimer = null;
                if (hackIE) { info.animate({ top: '+=' + distance + 'px' }, time, 'easeout', function() { shown = false; info.css('display', 'none'); }); }
                else { info.animate({ top: '+=' + distance + 'px', opacity: 0 }, time, 'easeout', function() { shown = false; info.css('display', 'none'); }); }
            }, 0);
        }
    });

    $('.btnSubscribe').click(function() {

        var strName = $('#txt_name').val();
        var strEmail = $('#txt_email').val();
        // alert('Page.aspx?name=' + strName + '&email' + strEmail);

        if (validate_details())
            window.location = location.href + '?name=' + strName + '&email=' + strEmail;
        return false;
    });

    $('#txt_name').click(function() {
        if ($('#txt_name').val() == 'Your Name')
            $(this).val('');
    });


    $('#txt_email').click(function() {
        if ($('#txt_email').val() == 'Your Email Address')
            $(this).val('');
    });

    if ($('.pics').find('img').size() >= 1) {

        if ($('.pics').find('img').size() > 1)
            $('.pics').cycle({ fx: 'fade', speed: 500, timeout: 5000, next: '.pics', pause: 1 });

    }


});


function browserLessThanIE8() { return (/MSIE ((5\\.5)|6|7)/.test(navigator.userAgent) && navigator.platform == "Win32"); }

function same_shipping() {
    var obj;
    obj = document.getElementById('ctl00_phContent_cbx_delivery_addr_same');
    if (obj.checked == false) {
        $('#ctl00_phContent_sa_first_name').removeAttr('disabled');
        $('#ctl00_phContent_sa_last_name').removeAttr('disabled');
        $('#ctl00_phContent_sa_address1').removeAttr('disabled');
        $('#ctl00_phContent_sa_address2').removeAttr('disabled');
        $('#ctl00_phContent_sa_suburb').removeAttr('disabled');
        $('#ctl00_phContent_sa_state').removeAttr('disabled');
        $('#ctl00_phContent_sa_phone').removeAttr('disabled');
        $('#ctl00_phContent_sa_postcode').removeAttr('disabled');
    } else {
        $('#ctl00_phContent_sa_first_name').val($('#ctl00_phContent_ba_first_name').val());
        $('#ctl00_phContent_sa_first_name').attr('disabled', true);
        $('#ctl00_phContent_sa_last_name').val($('#ctl00_phContent_ba_last_name').val());
        $('#ctl00_phContent_sa_last_name').attr('disabled', true);
        $('#ctl00_phContent_sa_address1').val($('#ctl00_phContent_ba_address1').val());
        $('#ctl00_phContent_sa_address1').attr('disabled', true);
        $('#ctl00_phContent_sa_address2').val($('#ctl00_phContent_ba_address2').val());
        $('#ctl00_phContent_sa_address2').attr('disabled', true);
        $('#ctl00_phContent_sa_suburb').val($('#ctl00_phContent_ba_suburb').val());
        $('#ctl00_phContent_sa_suburb').attr('disabled', true);


        var ba_state = $('#ctl00_phContent_ba_state').val();

        switch (ba_state) {
            case "NSW":
                $('#ctl00_phContent_sa_state').val('NSW Metro');
                break;
            case "VIC":
                $('#ctl00_phContent_sa_state').val('VIC');
                break;
            case "QLD":
                $('#ctl00_phContent_sa_state').val('QLD Metro');
                break;
            case "SA":
                $('#ctl00_phContent_sa_state').val('SA Metro');
                break;
            case "WA":
                $('#ctl00_phContent_sa_state').val('WA Metro');
                break;
            case "TAS":
                $('#ctl00_phContent_sa_state').val('TAS');
                break;
            case "NT":
                $('#ctl00_phContent_sa_state').val('NT');
                break;
            default:
                break;

        }

        /*$('#ctl00_phContent_sa_state').val($('#ctl00_phContent_ba_state').val());*/
        /*$('#ctl00_phContent_sa_state').attr('disabled', true);*/
        $('#ctl00_phContent_sa_postcode').val($('#ctl00_phContent_ba_postcode').val());
        $('#ctl00_phContent_sa_postcode').attr('disabled', true);
        $('#ctl00_phContent_sa_phone').val($('#ctl00_phContent_ba_phone').val());
        $('#ctl00_phContent_sa_phone').attr('disabled', true);
    }
}

function validate_details() {
    var errorMsg = '';

    if ($('#txt_name').val() == '') {
        errorMsg += '- Your name. \n';
    }

    if ($('#txt_email').val() == '') {
        errorMsg += '- Email. \n';
    }

    if ($('#txt_email').val() != '') {
        if (echeck($('#txt_email').val()) == false) {
            errorMsg += "- Please enter a valid Email Address. \n";
            $('#txt_email').focus();
        }
    }

    if (errorMsg != '') {
        errorMsg = "Please enter following fields: \n" + errorMsg;
        alert(errorMsg);
        return false;
    } else
        return true;
}

function echeck(str) {
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false
    }
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {

        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {

        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {

        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {

        return false
    }

    if (str.indexOf(" ") != -1) {

        return false
    }
}


