$(document).ready(function()
{

    jQuery.browser.msie6 = jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && !window["XMLHttpRequest"];

    var footer = $("#footer");
    if (footer.length > 0) {
        var off = footer.offset();
        $("#footer_bg").css('top', off.top);
        //$("#footer_bg").css('height', '50px');
    }
    
    var colour_image = jQuery(".colour_image a img");
    if (colour_image.length > 0 && jQuery.browser.msie6) {
        colour_image.each(function(i){
            jQuery(this).attr('src',jQuery(this).attr('alt'));
        });
    }

	var div_empty;
	$('div.hide_empty').each(function(i){
		div_empty = $(this);
		div_empty.children('div.inner').each(function(i){
			var inner_html = $(this).html();
			//alert('Box '+inner_html);
			var inner_html_trimmed = inner_html.replace(/^\s+|\s+$/g, '');
			if (inner_html_trimmed == '')
			{
				div_empty.remove();
			} else {
			    div_empty.show();
			}
		});
	});

    $('div.box').corner("round 7px top");

    jQuery("#image_carousel").jcarousel({
        scroll: 3,
        //start: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

    /* product display screen */
    var image_carousel_links = $('#image_carousel ul li a');
    if (image_carousel_links.length > 0) {
    	image_carousel_links.hover(
            function () {
                var a_id = $(this).attr('id').replace('img', '');
                changeImageInformation(a_id);
            },
            function () {

            }
    	);
    	image_carousel_links.each(function () {
    	   jQuery("<img>").attr("src", $(this).attr('href'));
    	});
    }

});
/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
    //jQuery('.jcarousel-control a').bind('click', function() {
    //    carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
    //    return false;
    //});

    //jQuery('.jcarousel-scroll select').bind('change', function() {
    //    carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
    //    return false;
    //});

    jQuery('#image_carousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#image_carousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};
function lightboxStartCallback(lightbox) {
    changeImageInformation(lightbox_settings.activeImage);
}
function lightboxFinishCallback(lightbox_settings) {
    changeImageInformation(lightbox_settings.activeImage);
}
function changeImageInformation (a_id) {
    var image_carousel_main = $('#ecom_product_image_main');
    var image_carousel_current = $('#img' + a_id);
    var image_carousel_main_desc =  $('#ecom_product_description p.desc_sub');
    var image_carousel_main_price = $('#ecom_product_price p span');
    var image_carousel_main_id = $('#selection_id');

    if (image_carousel_current.length == 0) {
        return false;
    }

    var title_id = 'title' + a_id;
    var desc_id = 'desc' + a_id;
    var price_id = 'price' + a_id;
    var id_id = 'id' + a_id;
    var tit = $('#'+title_id).val();
    var desc = $('#'+desc_id).val();
    if (tit == '' && desc == '') {
        image_carousel_main_desc.hide();
    } else {
        var desc_html = '{<span>' + tit + '</span>} ' + desc;
        image_carousel_main_desc.html(desc_html);
        image_carousel_main_desc.show();
    }
    image_carousel_main_price.html($('#'+price_id).val());
    image_carousel_main_id.val($('#'+id_id).val());
}
function clearValue(clearInput, defaultValue)
{
    if (clearInput.value == defaultValue) clearInput.value = '';
}
function showHide (showOrHide, elementToChange) {
    if (showOrHide == 'show')
    {
        $('#'+elementToChange).show();
    }
    else
    {
        $('#'+elementToChange).hide();
    }
}

function openUrl(url)
{
	window.open(url, '_self');
}
function openUrlFromSelect(select)
{
	var url = select.options[select.options.selectedIndex].value;
	openUrl(url);
}
function submitOnChange(idForm)
{
    $('#' + idForm).submit();
}


function ccTypeSelectShowHide(selectOrValue)
{
    if (typeof(selectOrValue) == 'string') {
        var val = selectOrValue;
    } else if (selectOrValue != undefined) {
        var val = selectOrValue.options[selectOrValue.options.selectedIndex].value;
    } else {
        var val = 'rubbish';
    }

    if (
        val == 'Solo'
        || val == 'Switch'
        || val == 'American Express'
    ) {
        $('#ecom_cc_extra').show();
        if (val != 'American Express') {
            $('#ecom_cc_extra_issue').show();
        } else {
            $('#ecom_cc_extra_issue').hide();
        }
    } else {
        $('#ecom_cc_extra_issue').hide();
        $('#ecom_cc_extra').hide();
    }
}
