function openPopup(url, w, h) {
    width = w;
    height = h;
    x = parseInt(screen.width / 2.0) - (width / 2.0);
    y = parseInt(screen.height / 2.0) - (height / 2.0);

    var win = window.open(url, "editorPopup", "top=" + y + ",left=" + x + ",scrollbars=no,dialog=yes,minimizable=no,modal=yes,width=" + width + ",height=" + height + ",resizable=no");
}

function deleteZoneContent(ZoneArea, PageID) {

    ret = vmcms.DeleteContent.DeleteZone(ZoneArea, PageID, OnComplete, OnTimeOut, OnError);
    return (true);

}

function OnComplete(args) {
    alert(args);
}

function OnTimeOut(args) {
    alert("ERROR : Service call timed out. Possbily content area has no content.");

}

function OnError(args) {
    alert("Error calling service method.");
}

jQuery(document).ready(function ($) {

    Cufon.replace('div#hpColLeft h2, div#hpColMiddle h2, div#hpColRight h2');

    $('.feedback').maxlength({
        events: [], // Array of events to be triggerd    
        maxCharacters: 100, // Characters limit   
        status: true, // True to show status indicator bewlow the element    
        statusClass: "status", // The class on the status div  
        statusText: "characters left", // The status text  
        notificationClass: "notification", // Will be added when maxlength is reached  
        showAlert: false, // True to show a regular alert message    
        alertText: "You have typed too many characters.", // Text in alert message   
        slider: true // True Use counter slider    
    });

    $('.insurance').maxlength({
        events: [], // Array of events to be triggerd    
        maxCharacters: 150, // Characters limit   
        status: true, // True to show status indicator bewlow the element    
        statusClass: "status", // The class on the status div  
        statusText: "characters left", // The status text  
        notificationClass: "notification", // Will be added when maxlength is reached  
        showAlert: false, // True to show a regular alert message    
        alertText: "You have typed too many characters.", // Text in alert message   
        slider: true // True Use counter slider    
    });

    $('.itemTitle').maxlength({
        events: [], // Array of events to be triggerd    
        maxCharacters: 50, // Characters limit   
        status: true, // True to show status indicator bewlow the element    
        statusClass: "status", // The class on the status div  
        statusText: "characters left", // The status text  
        notificationClass: "notification", // Will be added when maxlength is reached  
        showAlert: false, // True to show a regular alert message    
        alertText: "You have typed too many characters.", // Text in alert message   
        slider: true // True Use counter slider    
    });

    $('.itemDescription').maxlength({
        events: [], // Array of events to be triggerd    
        maxCharacters: 50, // Characters limit   
        status: true, // True to show status indicator bewlow the element    
        statusClass: "status", // The class on the status div  
        statusText: "characters left", // The status text  
        notificationClass: "notification", // Will be added when maxlength is reached  
        showAlert: false, // True to show a regular alert message    
        alertText: "You have typed too many characters.", // Text in alert message   
        slider: true // True Use counter slider    
    });

    $('ul.mainMenu li:first a').mouseover(function () {
        $(this).addClass('first');
    });
    $('ul.mainMenu li:first').css('border-left', '0px');

    $('ul.mainMenu li:first a').mouseout(function () {
        $(this).removeClass('first');
        $('ul.mainMenu li.on:first a').addClass('first');
    });

    $('ul.mainMenu li.on:first a').addClass('first');
    $('ul#slide').cycle({
        fx: 'fade',
        pause: 1
    });

    $('ul.register').cycle({
        fx: 'scrollHorz',
        timeout: 0,
        prev: '#prev',
        next: '#next',
        pager: '#nav',
        pagerAnchorBuilder: function (idx, slide) {
            return '<li><a href="#">Step ' + parseFloat(idx + 1) + '</a></li>';
        }
    });

    $('#loginBut').click(function () {
        $('#login').slideToggle('slow', function () {
            $('#form').fadeIn(400);
        });
    });

    $('.finishBut').click(function () {
        if (document.getElementById('ctl00_ContentPlaceHolder1_termsAgree').checked == false) {
            alert("Please agree to the Terms and Conditions.");
            return false;
        }
    });

    $('a.hideMap').click(function () {
        $('.mapRow').children('td').children('div').slideUp();
    });

    $('#mapDiv').slideUp();

    $('.hpTB').watermark('Town or Postcode');
    //$('div#header h1').snowfall

    $('input#ctl00_ContentPlaceHolder1_CreateUserWizard1_CreateUserStepContainer_courier').click(function () {
        $('div.optionalPanel').show();
        $('#ctl00_ContentPlaceHolder1_CreateUserWizard1_CreateUserStepContainer_details').slideDown('slow');
        $('#ctl00_ContentPlaceHolder1_CreateUserWizard1_CreateUserStepContainer_h2personal').removeClass('off');
        $('#ctl00_ContentPlaceHolder1_CreateUserWizard1_CreateUserStepContainer_h2personal').addClass('on');
    });
    $('input#ctl00_ContentPlaceHolder1_CreateUserWizard1_CreateUserStepContainer_sender').click(function () {
        $('div.optionalPanel').hide();
        $('#ctl00_ContentPlaceHolder1_CreateUserWizard1_CreateUserStepContainer_details').slideDown('slow');
        $('#ctl00_ContentPlaceHolder1_CreateUserWizard1_CreateUserStepContainer_h2personal').removeClass('off');
        $('#ctl00_ContentPlaceHolder1_CreateUserWizard1_CreateUserStepContainer_h2personal').addClass('on');
    });

    //$('input.eBayBut').click(function () {
    //if ($('#ctl00_ContentPlaceHolder1_ctl00_eBayID').val().length > 0) {
    //$(this).attr("style", "display:none");
    //$('span.wait').html("<img src=\"/graphics/loadingebay.gif\" alt=\"Loading, please wait...\"/>")
    //}
    //});

    $('.showBids').click(function () {
        $('h4#bidHistory').next().slideToggle('slow');
        $('h4#bidHistory').toggleClass('on');
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
 && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target
   || $('[name=' + this.hash.slice(1) + ']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body')
  .animate({ scrollTop: targetOffset }, 1000);
                if ($('.showBids').attr('href') == "#bids") {
                    $('.showBids').attr('href', '#top')
                } else {
                    $('.showBids').attr('href', '#bids')
                }
                return false;
            }
        }
    });

    $('div#description').expander({
        slicePoint: 440,  // default is 100
        expandText: '[read more]', // default is 'read more...'
        collapseTimer: 0, // re-collapses after 5 seconds; default is 0, so no re-collapsing
        userCollapseText: '[less]'  // default is '[collapse expanded text]'
    });

    $('div#hpColLeft p').expander({
        slicePoint: 680,  // default is 100
        expandText: '[read more]', // default is 'read more...'
        collapseTimer: 0, // re-collapses after 5 seconds; default is 0, so no re-collapsing
        userCollapseText: '[less]'  // default is '[collapse expanded text]'
    });

    $('.showActionBids').click(function () {
        $(this).parent().parent().next().children('td').children('div').slideToggle('slow');
    });

    $('.messages tr').click(function () {
        $(this).next().children('td').children('div').slideToggle('slow');
    });

    //$('table.messages tr:last-child td div').show();

    $('.detailsBottom h4').click(function () {
        $(this).next().slideToggle('slow');
        $(this).toggleClass('on');
    });

    $('h2.off').click(function () {
        $(this).next().slideToggle('slow');
        $(this).addClass('on');
        $(this).removeClass('off');
    });
    $('h2.on').click(function () {
        $(this).next().slideToggle('slow');
        $(this).addClass('off');
        $(this).removeClass('on');
    });

    $('h3.off').click(function () {
        $(this).next().slideToggle('slow');
        $(this).toggleClass('on', 'off');
    });
    $('h3.on').click(function () {
        $(this).next().slideToggle('slow');
        $(this).toggleClass('on', 'off');
    });

    $('h5.off').click(function () {
        $(this).next().slideToggle('slow');
        $(this).toggleClass('on', 'off');
    });
    $('h5.on').click(function () {
        $(this).next().slideToggle('slow');
        $(this).toggleClass('on', 'off');
    });

    $('input.showwait').click(function () {
        $('#progress').attr("style", "display:block");
    });

    $('select.showwait').change(function () {
        $('#progress').attr("style", "display:block");
    });

    $('input.showwait').change(function () {
        $('#progress').attr("style", "display:block");
    });

    $('span.showwait input').change(function () {
        $('#progress').attr("style", "display:block");
    });

    $('span.showwait input').click(function () {
        $('#progress').attr("style", "display:block");
    });

    $('a.showwait').click(function () {
        $('#progress').attr("style", "display:block");
    });

    $('tr.showwait').click(function () {
        $('#progress').attr("style", "display:block");
    });

    $('.area').watermark('Town or Postcode');

    $('#ctl00_ContentPlaceHolder1_ctl00_ddlCat').change(function () {
        if ($(this).val() != $('#ctl00_ContentPlaceHolder1_ctl00_hidCat').val()) {
            $("#ctl00_ContentPlaceHolder1_ctl00_ddlSubCat").val($("#ctl00_ContentPlaceHolder1_ctl00_ddlSubCat option:first").val());
            $('#ctl00_ContentPlaceHolder1_ctl00_ddlSubCat').attr('disabled', 'disabled');
        } else {
            $('#ctl00_ContentPlaceHolder1_ctl00_ddlSubCat').removeAttr('disabled');
        }
        $(this).val($(this).val());
    });

    $('ul#thumbs li a').hover(function () {
        $('#mainImg img').attr("src", $(this).attr('href').replace("xl", "main"));
    });

    $('a[rel=gallery]').colorbox();

    $(".bidButton").colorbox({ width: "470px", height: "380px", iframe: true });

    $(".contactLink").colorbox({ width: "470px", height: "380px", iframe: true });



    jQuery(".usernameRegister").keyup(function () {
        var searchbox = $(this).val();
        var dataString = 'k=' + searchbox;
        if (searchbox == '')
        { jQuery("#display").hide(); }
        else {
            jQuery.ajax({
                type: "POST",
                url: "/my-account/username-check.aspx",
                data: dataString,
                cache: false,
                success: function (html) {
                    jQuery("#display").html(html).show();
                }
            });
        } return false;
    });

});

var jump = function (e) {
    //prevent the "normal" behaviour which would be a "hard" jump
    e.preventDefault();
    //Get the target
    var target = $(this).attr("href");
    //perform animated scrolling
    $('html,body').animate(
       {
           //get top-position of target-element and set it as scroll target
           scrollTop: $(target).offset().top
           //scrolldelay: 2 seconds
       }, 2000, function () {
           //attach the hash (#jumptarget) to the pageurl
           location.hash = target;
       });

}


function showMap() {
    $('.mapRow').slideToggle('slow');
}

function showCollectionAddresses() {
    $('#collectAddress').show();
    var cp_col = CraftyPostcodeCreate();
    // generic setup
    cp_col.set("access_token", cp_access_token);
    cp_col.set("first_res_line", "----- please select your address ----");
    cp_col.set("res_autoselect", "0");
    cp_col.set("result_elem_id", "collectAddress");
    cp_col.set("form", "aspnetForm");
    cp_col.set("elem_company", "ctl00_ContentPlaceHolder1_ctl00_collectCompany"); // optional
    cp_col.set("elem_street1", "ctl00_ContentPlaceHolder1_ctl00_collectAdd1");
    cp_col.set("elem_street2", "ctl00_ContentPlaceHolder1_ctl00_collectAdd2"); // optional, but highly recommended
    cp_col.set("elem_street3", "ctl00_ContentPlaceHolder1_ctl00_collectAdd3"); // optional
    cp_col.set("elem_town", "ctl00_ContentPlaceHolder1_ctl00_collectTown");
    cp_col.set("elem_county", "ctl00_ContentPlaceHolder1_ctl00_collectCounty"); // optional
    cp_col.set("elem_postcode", "ctl00_ContentPlaceHolder1_ctl00_collectPostcode");
    cp_col.set("single_res_autoselect", 1); // don't show a drop down box if only one matching address is found
    // house name/num search setup
    cp_col.set("elem_search_house", "search_string");
    cp_col.doLookup();
    //$.fn.colorbox({ 'href': '#collectAddress', 'open': false, 'inline': true, 'width': '600px', 'height': '100px' });
    //$().bind('cbox_closed', function () {
        //$('#collectAddress').hide();
    //});
}

function showDeliveryAddresses() {
    cp_del.doLookup();
    $('#deliveryAddress').show();
    $.fn.colorbox({ 'href': '#deliveryAddress', 'open': false, 'inline': true, 'width': '600px', 'height': '100px' });
    $().bind('cbox_closed', function () {
        $('#deliveryAddress').hide();
    });
}

function watermark() {
    $('.hpTB').watermark('Town or Postcode');
}

function resultChange() {
    parent.$.fn.colorbox.close();
}
