﻿$(document).ready(function() {

    // AJAX webservice query
     
    var productID = $("input.hidden:eq(0)").val();
    var currentUrl = $("input.hidden:eq(1)").val();
    
    getPromoObjects();
    
    function getPromoObjects()
    {
        try
        {
            RegionalPromotions.RegionalPromotionService.GetRegionalPromotions(productID,currentUrl,testSucceeded,testFailed,null);
        }
        catch(error)
        {
            //alert(error);
        }
    }
    
    function parseString(myString)
    {
        myString = myString.replace(/'/,"&#39;");
        myString = myString.replace(/"/,"&#34;");
        return myString;
    }

    function testSucceeded(value,context)
    {
        var graphicSrc;
        var graphicWidth;
        var graphicHeight;
        var imageFormat;
        var promoText;
        
        for(var i = value.length-1; i >= 0; i--) // Insert accordion markup for every object returned from AJAX call
        {   
            if(value.length == 1)
            {
                graphicSrc = value[i].LargeUrl;
                graphicWidth = value[i].LargeWidth;
                graphicHeight = value[i].LargeHeight;
                imageFormat = value[i].LargeType;
                promoText = parseString(value[i].LargeText);
            }
            else
            {
                graphicSrc = value[i].SmallUrl;
                graphicWidth = value[i].SmallWidth;
                graphicHeight = value[i].SmallHeight;
                imageFormat = value[i].SmallType;
                promoText = parseString(value[i].SmallText);
            }
            
            if (imageFormat == 1)
            {
                $("#divRegionalPromotions h3").after("<h4>" + value[i].Title + "</h4><a href='" + value[i].Url + "'><img src='" + graphicSrc + "' width='" + graphicWidth + "' height='" + graphicHeight + "' class='graphic' alt='"+ promoText +"' /></a>");
            }         
            else if (imageFormat == 2)
            {
                if ($.browser.msie)
                {   
                    objEmbed = document.createElement('embed');
                    objEmbed.setAttribute('src',graphicSrc);
                    objEmbed.setAttribute('class','graphic');
                    objEmbed.setAttribute('width',graphicWidth);
                    objEmbed.setAttribute('height',graphicHeight);
                    objEmbed.setAttribute('flashvars','myUrl=' + value[i].Url);
                    $("#divRegionalPromotions h3").after("<h4>" + value[i].Title + "</h4>").next().after(objEmbed);
                    $(objEmbed).wrap("<span class='graphic'></span>");
                }
                else
                {
                    $("#divRegionalPromotions h3").after("<h4>" + value[i].Title + "</h4><span class='graphic'><object width='" + graphicWidth + "' height='" + graphicHeight + "'><param value='" + graphicSrc + "' /><embed src='" + graphicSrc + "' width='" + graphicWidth + "' height='" + graphicHeight + "' flashvars='myUrl=" + value[i].Url + "'></embed></object></span>");
                }
            }
            else
            {
                $("#divRegionalPromotions h3").after("<h4>" + value[i].Title + "</h4><a href='" + value[i].Url + "'><div class='graphic'>" + promoText + "</div></a>");
            }

            $("#divRegionalPromotions h3").fadeIn("slow");
            $("#divRegionalPromotions h4").slideDown("slow",function() {
                $("#divRegionalPromotions .graphic:eq(0)").addClass("active");
                $("#divRegionalPromotions h4:eq(0)").addClass("active");
            });
        }

        // Accordion behavior
        
        var objDivRegionalPromotions = document.getElementById("divRegionalPromotions");
        var arrayDivRegionalPromotionsH4 = objDivRegionalPromotions.getElementsByTagName("h4");
        var arrayDivRegionalPromotionsGraphic = $(".graphic");
        
        var openAd = 0;
        
        for (var i = 0; i < arrayDivRegionalPromotionsH4.length; i++)
        {
            arrayDivRegionalPromotionsH4[i].h4num = i;
            
            arrayDivRegionalPromotionsH4[i].onmouseover = function()
            {
                $(this).addClass("hover");
            }
            
            arrayDivRegionalPromotionsH4[i].onmouseout = function()
            {
                $(this).removeClass("hover");
            }
            
            arrayDivRegionalPromotionsH4[i].onclick = function()
            {
                if(this.h4num != openAd)
                {
                    myh4Num = this.h4num;
                    $(arrayDivRegionalPromotionsH4[openAd]).removeClass("active");
                    $(arrayDivRegionalPromotionsH4[myh4Num]).addClass("active");
                    $(arrayDivRegionalPromotionsGraphic[openAd]).removeClass("active");
                    $(arrayDivRegionalPromotionsGraphic[myh4Num]).addClass("active");
                    openAd = myh4Num;
                }
            }
        }
    }
    
    // This is the failed callback function.
    function testFailed(error)
    {
        /*var stackTrace = error.get_stackTrace();
        var message = error.get_message();
        var statusCode = error.get_statusCode();
        var exceptionType = error.get_exceptionType();
        var timedout = error.get_timedOut();
       
        // Display the error.    
        var myError = 
            "Stack Trace: " +  stackTrace + ", " +
            "Service Error: " + message + ", " +
            "Status Code: " + statusCode + ", " +
            "Exception Type: " + exceptionType + ", " +
            "Timedout: " + timedout;
            alert(myError);
            */
    }
});
