
function changeLogoPositionInFlash(position) {
    thisMovie("be_a_bag").selectLogoPosition(position);
    //flashMovie.selectLogoPosition(position);

    var topPositionCheck = document.getElementById("top-position");
    var bottomPositionCheck = document.getElementById("bottom-position");

    if (position == 'top') {
        bottomPositionCheck.checked = false;
    }
    else {
        topPositionCheck.checked = false;
    }
}

function sendImagesToServer(position) {
    thisMovie("be_a_bag").selectLogoPosition2(position);
}

function changeColourInFlash(colour) {
    thisMovie("be_a_bag").selectColour(colour);
    //flashMovie.selectColour(colour);
}

function thisMovie(movieName) {
    return document.getElementById(movieName);
}

function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}

function callToActionscript(str) {

    getFlashMovie("be_a_bag").sendToActionscript();
}

var SITE = SITE || {};

SITE.fileInputs = function() {
    var $this = jQuery(this),
      $val = $this.val(),
      valArray = $val.split('\\'),
      newVal = valArray[valArray.length - 1],
      $button = $this.siblings('.button'),
      $fakeFile = $this.siblings('.file-holder')
    $noFile = $this.siblings('.no-file')
    if (newVal !== '') {
        var imageChosenText = document.getElementById("hdImageChosenText");
        $button.text(imageChosenText.value);
        $button.width("111px");
        if ($fakeFile.length === 0) {
            $noFile.hide();

            if (valArray[valArray.length - 1].length > 21) {
                var concatenatedNewVal = valArray[valArray.length - 1].substring(0, 20) + '...';
                $button.after('<span width="200px" class="file-holder">' + concatenatedNewVal + '</span>');
            }
            else {
                $button.after('<span width="200px" class="file-holder">' + newVal + '</span>');
            }
        } else {
            if (valArray[valArray.length - 1].length > 21) {
                var concatenatedNewVal = valArray[valArray.length - 1].substring(0, 20) + '...';
                $fakeFile.text(concatenatedNewVal);
            }
            else {
                $fakeFile.text(newVal);
            }
        }
    }
};

jQuery(document).ready(function() {
    jQuery('.file-wrapper input[type=file]').bind('change focus click', SITE.fileInputs);
    jQuery('.file-wrapper2 input[type=file]').bind('change focus click', SITE.fileInputs);

    if (jQuery('#divSecondImagePrice').length != 0) {
        jQuery('#divSecondImagePrice').html('+ ' + currencySymbol + '00.00');
    }

    jQuery("a#ctl00_contentPlaceHolderListing_BaBControl_hidden_link").fancybox();
    jQuery("#lnkProcessImages").fancybox({
        "showCloseButton": false,
        "enableEscapeButton": false,
        "hideOnOverlayClick": false
    });

    jQuery("#ShowEmbossingExamples").fancybox({
        'scrolling': 'yes',
        'autoDimensions': false,
        'height': 600,
        'width': 800,
        'onComplete': function() {
            jQuery("#fancybox-inner").css({ 'overflow-x': 'hidden' });
        }
    });
});

var BaB = {
    Init: function() {

        // Load dialog on click
        jQuery("#ShowBespokeExamples").fancybox({
            'scrolling': 'yes',
            'autoDimensions': false,
            'height': 600,
            'width': 800,
            'onComplete': function() {
                jQuery("#fancybox-inner").css({ 'overflow-x': 'hidden' });
            }
        });

        var numberOfImages = document.getElementById('hdnNumberofImages');
        var postImages = document.getElementById('postImages');
        var chkDifferentPhoto = document.getElementById('chkDifferentPhoto');
        var chkPostPhoto = document.getElementById('chkPostPhoto');
        var trPostPhotoInformation = document.getElementById('trPostPhotoInformation');


        // preset the fields if we are looking at an existing basket detail
        if (numberOfImages != null && numberOfImages.value == 2) {
            chkDifferentPhoto.checked = true;
            BaB.ToggleUseTwoPhotos();
        }

        if (postImages != null && postImages.value == "True") {
            chkPostPhoto.checked = true;
            trPostPhotoInformation.style.display = "block";
            jQuery('#divChoosePhoto :input').attr('disabled', true);
            jQuery('#divChoosePhoto2 :input').attr('disabled', true);
        }
        else {
            chkPostPhoto.checked = false;
            trPostPhotoInformation.style.display = "none";
            jQuery('#divChoosePhoto :input').attr('disabled', false);
            jQuery('#divChoosePhoto2 :input').attr('disabled', false);
        }
    },
    CalculatePrice: function() {
        var differentPhotoForSecondImage = document.getElementById('chkDifferentPhoto');

        var data = '{currencyCode: "' + currency + '", productColourId: ' + productColourId + ', hasTwoImages: ' + differentPhotoForSecondImage.checked + '}';

        new Ajax.Request('/WebServices/PriceLookup.asmx/GetPriceWithPersonalisations', {
            method: 'post',
            contentType: 'application/json',
            postBody: data,
            onSuccess: function(transport, json) {
                priceElement.innerHTML = transport.responseText.replace(/\"/g, "");
                priceElement.innerHTML = priceElement.innerHTML.replace("{", "");
                priceElement.innerHTML = priceElement.innerHTML.replace("}", "");
                // JDG 28 June 2010
                // for some reason we have to reset the style clear value AFTER its loaded
                //priceElement.style.clear = "";
            },
            onException: function(req, exception) {
                alert('The request had a fatal exception thrown.\n\n' + exception);
                return true;
            },
            onFailure: function(request) {
                //alert('failture' + request.responseText);
            }
        });
    },
    CreateSummary: function() {
        var differentPhotoForSecondImage = document.getElementById('chkDifferentPhoto');

        var html = '<table width="160px" cellpadding="0" cellspacing="0"><tr><td align="right" width="30px" class="lightHorizontalPadding lightRightPadded" height="25px"><strong>TOTAL</strong></td><td width="10px" class="leftBorderDark"></td><td class="lightHorizontalPadding" align="left" style="padding-left:8px"  height="23px">PRICE:</td></tr>';

        html += '<tr><td align="right" width="30px" class="lightHorizontalPadding lightRightPadded" height="25px">Product</td><td width="10px" class="leftBorderDark"></td><td width="40px" align="right" class="lightHorizontalPadding">+ ' + formattedProductPrice + '</td></tr>'

        if (differentPhotoForSecondImage.checked) {
            var totalPriceDec = parseFloat(totalPrice).toFixed(2);
            var extraPriceDec = parseFloat(differentImageCost).toFixed(2);

            html += '<tr><td align="right" width="30px" class="lightHorizontalPadding lightRightPadded"></td><td width="10px" class="leftBorderDark"></td><td width="40px" align="right" class="lightHorizontalPadding">+ ' + currencySymbol + parseFloat(differentImageCost).toFixed(2) + '</td></tr>';
            html += '<tr><td align="right" width="30px" class="lightHorizontalPadding lightRightPadded" height="5px"></td><td width="10px" class="leftBorderDark"></td><td width="40px" align="left"></td></tr>';
            html += '<tr><td align="right" width="30px" class="lightHorizontalPadding lightRightPadded" height="5px"></td><td width="10px" class="leftBorderDark"></td><td width="40px" align="left" class="borderTop"></td></tr>';
            html += '<tr><td align="right" width="30px" class="lightHorizontalPadding lightRightPadded"></td><td width="10px" class="leftBorderDark"></td><td width="40px" align="right" class="lightHorizontalPadding">+ ' + currencySymbol + parseFloat(parseFloat(totalPriceDec) + parseFloat(extraPriceDec)).toFixed(2) + '</td></tr>';
        }
        else {
            html += '<tr><td align="right" width="30px" class="lightHorizontalPadding lightRightPadded" height="5px"></td><td width="10px" class="leftBorderDark"></td><td width="40px" align="left"></td></tr>';
            html += '<tr><td align="right" width="30px" class="lightHorizontalPadding lightRightPadded" height="5px"></td><td width="10px" class="leftBorderDark"></td><td width="40px" align="left" class="borderTop"></td></tr>';
            html += '<tr><td align="right" width="30px" class="lightHorizontalPadding lightRightPadded"></td><td width="10px" class="leftBorderDark"></td><td width="40px" align="right" class="lightHorizontalPadding">+ ' + formattedProductPrice + '</td></tr>';
        }

        html += '<tr><td colspan="3" height="10px"></td></tr>';

        html += '</table>';

        document.getElementById('BaBSummary').innerHTML = html;
    },
    TogglePhotosPrice: function() {
        this.CalculatePrice();
        this.CreateSummary();
    },
    TogglePostPhoto: function() {
        var postPhotoCheckBox = document.getElementById('chkPostPhoto');
        var samePhotoForSecondImage = document.getElementById('chkSamePhoto');
        var differentPhotoForSecondImage = document.getElementById('chkDifferentPhoto');
        var trPostPhotoInformation = document.getElementById('trPostPhotoInformation');
        var divChoosePhoto = document.getElementById('divChoosePhoto');
        var divChoosePhoto2 = document.getElementById('divChoosePhoto2');

        if (postPhotoCheckBox.checked) {
            trPostPhotoInformation.style.display = "block";
            jQuery('#divChoosePhoto :input').attr('disabled', true);
            jQuery('#divChoosePhoto2 :input').attr('disabled', true);
        }
        else {
            trPostPhotoInformation.style.display = "none";
            jQuery('#divChoosePhoto :input').attr('disabled', false);
            jQuery('#divChoosePhoto2 :input').attr('disabled', false);
        }
    },
    ToggleUseOnePhoto: function() {
        var samePhotoForSecondImage = document.getElementById('chkSamePhoto');
        var differentPhotoForSecondImage = document.getElementById('chkDifferentPhoto');
        var divChoosePhoto2 = document.getElementById('divChoosePhoto2');

        if (samePhotoForSecondImage.checked) {
            differentPhotoForSecondImage.checked = false;
            divChoosePhoto2.style.display = "none";
        }
        else {
            differentPhotoForSecondImage.checked = true;
            divChoosePhoto2.style.display = "block";
        }

        this.TogglePhotosPrice();
    },
    ToggleUseTwoPhotos: function() {
        var samePhotoForSecondImage = document.getElementById('chkSamePhoto');
        var differentPhotoForSecondImage = document.getElementById('chkDifferentPhoto');
        var divChoosePhoto2 = document.getElementById('divChoosePhoto2');

        if (differentPhotoForSecondImage.checked) {
            samePhotoForSecondImage.checked = false;
            divChoosePhoto2.style.display = "block";
        }
        else {
            samePhotoForSecondImage.checked = true;
            divChoosePhoto2.style.display = "none";
        }

        this.TogglePhotosPrice();
    }
}

function showBabExamples() {
    jQuery('#basic-modal-content').modal({
});
}

function ProcessImages() {
    // execute serverside command to upload images and redirect to BaB creator
    jQuery("#ctl00_contentPlaceHolderListing_BaBControl_btnProcessImages").click();

    // show processing modal (cannot close)
    jQuery("#lnkProcessImages").click();
}
