// Declare global objects available to all javascript.
var _helper = new Helper();
var _store = new Store();

// Avoid conflict with prototype. Use $j for all jquery on this page.
var $j = jQuery.noConflict();
$j(document).ready(function() {

    _store.SetClassHelpers();

    // Home page rotating banner plugin.
    $j('[id$=pnlRBanner]').rbanner({
        timeout: 6000,
        pause: true
    });
    
    // Entity page rotating banner plugin.
    $j('#PageWrapper.Entity .BannersWrapper').rbanner({
        timeout: 4000,
        pause: true
    });
    
    // Call sliding boxes plugin.
    $j('#HomeImages .boxgrid').slidingboxes({
    });
    
    // Hover button plugin.
    $j('.HoverButton').hoverbutton();
    
    // Product page tab plugin.
    $j("#tabs").tabs();
    
    // Out of stock click event. Redirect to itemavailable.aspx.
    $j('#ProductWrapper .StockHint IMG').click(function() {
        var counter = $j('#Counter').val();
        var href = 'itemavailable.aspx?id=' + counter;
        window.location = href;
    });
    
    // Add testimonial speech bubble indicator.
    SetupTestimonials();
    
    // Setup press page.
    SetupPress();

});

function SetupTestimonials() {

    var i = 1;
    
    $j('#PageWrapper.customerfeedback .Testimonial').each(function() {
        $j(this).wrap('<div class="TestimonialWrapper T' + i++ + '" />').after('<div class="BubbleIndicator"></div>');
    });
}

function SetupPress() {

    if (_store.GetPageName() == 'press.aspx') {
    
        //Initialize PressItem dialog.
        $j("#PressItem1_Dialog").dialog({
            autoOpen: false,
            modal: true,
            resizable: true,
            width: 770,
	        height: 600,
	        dialogClass: "PressItem"
        });
        
        //Display PressItem dialog on button click.
        $j("IMG.Thumbnail, .PressItem .Source").click(function() {
            $j("#PressItem1_Dialog").dialog("open");
            //PressCode available as custom attribute on thumbnail image.
            GetPressItem($j(this).attr("presscode"));
        });
        
        //Close PressItem dialog on button click.
        $j("#PressItem1_lblClose").click(function() {
            $j("#PressItem1_Dialog").dialog("close");
        });
        
        //Show previous press item.
        $j("#PressItem1_lblPrevious, #PressItem1_imgPrevious").click(function() {
            if (aPressCode.length > 1) {
                index = index - 1;
                if (index < 0) {
                    index = aPressCode.length - 1;
                }
                GetPressItem(aPressCode[index]);
            }
        });
        
        //Show next press item.
        $j("#PressItem1_lblNext, #PressItem1_imgNext").click(function() {
            if (aPressCode.length > 1) {
                index = index + 1;
                if (index >= aPressCode.length) {
                    index = 0;
                }
                GetPressItem(aPressCode[index]);
            }
        });
    }
}

//Display press item using dynamic HTML.
function GetPressItem(PressCode) {
    
    //Get pipe separated PressCode values.
    aPressCode = $j("#hidPressCode").val();
    if (aPressCode != null) {
        aPressCode = aPressCode.split("||");
        for (var i=0; i<aPressCode.length; i++) {
            if (PressCode == aPressCode[i]) {
                //Get currently selected press code index.
                index = i;
            }
        }
    }
    
    if (index > -1) {
        //Get pipe separated data.
        var aImageFileNameSource = $j("#hidImageFileNameSource").val();
        var aImageFileNamePress = $j("#hidImageFileNamePress").val();
        var aSource = $j("#hidSource").val();

        //Split into array.
        aImageFileNameSource = aImageFileNameSource.split("||");
        aImageFileNamePress = aImageFileNamePress.split("||");
        aSource = aSource.split("||");
            
        //Dynamically set page data.
        $j("#PressItem1_imgThumbnail").attr("src", aImageFileNameSource[index]);
        $j("#PressItem1_imgPress").attr("src", aImageFileNamePress[index]);
        //$j("#PressItem1_lblSource").html(aSource[index]);
        $j(".ui-dialog-title").html(aSource[index]);
    }   
}
