/*
 * Trixan static JS for item.jsp
*/

var zoomstate = 0;

var zconfig = {
    zoomWidth: 428, // zooming div default width (default is 200)
    zoomHeight: 423, // zooming div default height (default is 200)
    xOffset: 15, // zooming div offset (default is 10)
    yOffset: 0,
    position: "right", // zooming dif position (default is "right")
    preload: true, // preload zoom image (default is 1 "true")
    title: true,
    showEffect: 'fadein',
    hideEffect: 'fadeout',
    fadeinSpeed: "medium",
    fadeoutSpeed: "slow",
    zoomType: "reverse",
    imageOpacity: 0.65
    //lens: 1 // lens over the image (default is 1 "true")
};

var availabilityTable = [{
    "id": "24hours",
    "label": "Usually dispatched within 24 hours"
}, {
    "id": "3days",
    "label": "Usually dispatched within 3 days"
}, {
    "id": "1week",
    "label": "Usually dispatched within 7 days"
}, {
    "id": "2weeks",
    "label": "Usually dispatched within 14 days"
}, {
    "id": "1month",
    "label": "Usually dispatched within 30 days"
}, {
    "id": "unavailable",
    "label": "Unavailable"
}, {
    "id": "none",
    "label": ""
}];

function AddFavorite_onclick(){
    BookmarkTitle = $('#item-name h1').html();
    BookmarkURL = document.location.href;

    if (window.sidebar) { // Bookmark for Mozilla Firefox
        window.sidebar.addPanel(BookmarkTitle, BookmarkURL, "");
    } else if (window.external) { // IE's Favorite
        window.external.AddFavorite(BookmarkURL, BookmarkTitle);
    } else if (window.opera && window.print) { // Opera Hotlist
        var element = document.createElement("a");
        element.setAttribute("href", BookmarkURL);
        element.setAttribute("title", BookmarkTitle);
        element.setAttribute("rel", "sidebar");
        element.click();
    }
}

function emailForm(){
    var email = "friend";
    var subject = $('#item-name h1').html();
    var body_message = "" + document.location.href;

    var mailto_link = 'mailto:' + email + '?subject=' + subject + '&body=' + body_message;

    win = window.open(mailto_link, 'emailWindow');
    if (win && win.open && !win.closed) { 
        win.close();
    }
}


function translateAvailability(id){
    var translatedText = id;
    var found = false;
    for (i = 0; i < availabilityTable.length; i++) {
        if (id == availabilityTable[i].id) {
            translatedText = availabilityTable[i].label;
            found = true;
            break;
        }		
    }
    if (!found) {
        translatedText = "Unknown";
    }
    return translatedText;
}

function showImage(imageToDisplayIndex){
    
	var skuroot = skus.colors[skus.displayedColor].sizes[skus.colors[skus.displayedColor].displayedSize];
	var imageToDisplay = skuroot.sku.images[imageToDisplayIndex];
    
    if (imageToDisplay.mediumImageURL != "") {
        $('#main_img').attr('src', imageToDisplay.mediumImageURL);
        if (imageToDisplay.largeImageURL != "") {
            $('.myzoom').attr("href", imageToDisplay.largeImageURL);
            $('.myzoom').attr("title", $('#item-name h1').html());
            if (zoomstate == 1) {
                $('.myzoom').jqzoom(zconfig);
            }
            else {
                $('.myzoom').attr('href', "javascript:void(0)");
            }
            
        }
        else {
            $('.myzoom').unbind();
            $('.myzoom').attr('href', "javascript:void(0)");
        }
        
    }
    
    skuroot.sku.displayedImage = imageToDisplayIndex;
}

function changeData(color, sizeIndex){
    var imageToDisplay = color.sizes[sizeIndex].sku.images[color.sizes[sizeIndex].sku.displayedImage];
    var skuroot = color.sizes[sizeIndex].sku;
	if (imageToDisplay.mediumImageURL != "") {
        $('#main_img').attr('src', imageToDisplay.mediumImageURL);
        
        if (imageToDisplay.largeImageURL != "") {
            $('.myzoom').attr('href', imageToDisplay.largeImageURL);
            $('.myzoom').attr("title", $('#item-name h1').html());
            if (zoomstate == 1) {
                $('.myzoom').jqzoom(zconfig);
            }
            else {
                $('.myzoom').attr('href', "javascript:void(0)");
            }
			$('#control-zoom a').removeClass('off');
        }
        else {
            $('.myzoom').unbind();
            $('.myzoom').attr('href', "javascript:void(0)");
			$('#control-zoom a').addClass('off');
        }
        
        $("#video div.content").empty();
        $("#video div.content").unbind();
        $("#controls #control-video a").unbind();
        if (imageToDisplay.videoImageURL != "") {
            var config = {
                src: imageToDisplay.videoImageURL,
                w3c: true
            };
            if ($.browser.msie) 
                config.w3c = false;
            $("#video div.content").flashembed(config);
            $("#controls #control-video a").click(function(){
                $("#video").jqmShow()
            });
			$("#controls #control-video a").show();
        } else {
			$("#controls #control-video a").hide();
		}
        
        $("#threed div.content").empty();
        $("#controls #control-threed a").unbind('click');
        if (imageToDisplay.threeDImageURL != "") {
            var config = {
                src: imageToDisplay.threeDImageURL,
                w3c: true
            };
            if ($.browser.msie) 
                config.w3c = false;
            $("#threed div.content").flashembed(config);
            $("#controls #control-threed a").click(function(){
                $("#threed").jqmShow()
            });
			$("#controls #control-threed a").show();
        } else {
			$("#controls #control-threed a").hide();
		}
        
        for (i = 0; i < skuroot.images.length; i++) {
            if (skuroot.images[i].thumbnailImageURL != "") {
                $('#alternate_' + (i + 1)).attr('src', skuroot.images[i].thumbnailImageURL);
                $('#imgLnk_' + (i + 1)).show();
            }
            else {
                $('#imgLnk_' + (i + 1)).hide();
                $('#alternate_' + (i + 1)).attr('src', '/static/images/spacer.gif');
            }
        }
        for (i = skuroot.images.length; i < 3; i++) {
            $('#imgLnk_' + (i + 1)).hide();
            $('#alternate_' + (i + 1)).attr('src', '/static/images/spacer.gif');
        }
		//alert(skuroot.images.length);
		if (skuroot.images.length == 1) {			// Hide the thumb if only one image
			$('#imgLnk_1').hide();
		} else {
			$('#imgLnk_1').show();
		}
    }

    if (skuroot.onSale == 'true') {
        $('#price').html(skuroot.salePricePrefix + ' : ' + skuroot.salePrice);
        $('#salePrice').text(skuroot.listPrice);
        $('#clearance').show();
    }
    else {
        $('#clearance').hide();
        $('#salePrice').html('&nbsp;');
        if (skuroot != '') {
        	$('#price').html(skuroot.listPricePrefix + ' : ' + skuroot.listPrice);
        }
        else {              
            $('#price').html('');
        }
    }
    
    if (skuroot.hasRetailPrice == 'true') {
        $('#regularPrice').text(skuroot.retailPrice);
        $('#regular').show();
    }
    else {
        $('#regular').hide();
        $('#regularPrice').html('&nbsp;');
    }
    
    if (skuroot.available == 'false' || skuroot.listPrice == '') {
        $('#add-cart').attr("disabled", true);
        $('#add-cart').hide();
    }
    else {
        $('#add-cart').removeAttr("disabled");
        $('#add-cart').show();
    }
    
    $('#skuId').val(skuroot.id);
    
    if (skuroot.listPrice == '' && skuroot.salePrice == ''){
    	if (skuroot.hasRetailPrice == 'true'){
        	$('#price').html( skuroot.retailPricePrefix + ' : ' + skuroot.retailPrice);
        }
        $('#regular').html( '' );
    } 
}

function changeSizeOption(selectedIndex){
    var colorIndex = $("#config-color").get(0).selectedIndex;
    if ((colorIndex > 0) && (selectedIndex > 0)) {
        skus.displayedColor = colorIndex - 1;
        skus.colors[skus.displayedColor].displayedSize = selectedIndex - 1;
        skus.colors[skus.displayedColor].sizes[skus.colors[skus.displayedColor].displayedSize].sku.displayedImage = 0;
        $('#availabilityValue').html(translateAvailability(skus.colors[skus.displayedColor].sizes[skus.colors[skus.displayedColor].displayedSize].sku.availability));
        changeData(skus.colors[skus.displayedColor], skus.colors[skus.displayedColor].displayedSize);
    }
    else {
        $('#availabilityValue').text('');
    }
}

function validateCart(){
    if (($("#config-size").get(0).selectedIndex == 0) ||
    ($("#config-color").get(0).selectedIndex == 0)) {
        alert('Please select a configuration.');
        return false;
    }
    else {
        document.addToCartForm.submit();
        return true;
    }
}


