﻿var I_DEMO_MOVIE_INCREASE_HEIGHT = 15;
var I_DEMO_MOVIE_INCREASE_INTERVAL = 30;

// After the menu is loaded, this function adds the rounded top and bottom lines
function addGalleryMenuCorners()
{
    var aElements = $$("#GalleryLeftMenu li a");
    var oTop = document.createElement("span")
        oTop.className = "top";
        oTop.innerHTML = "&#160;";
    var oBottom = document.createElement("span")
        oBottom.className = "bottom";
        oBottom.innerHTML = "&#160;";
    
    for(var i=0; i<aElements.length; i++)
    {
        aElements[i].appendChild(oTop.cloneNode(true));
        aElements[i].appendChild(oBottom.cloneNode(true));
    }
    
    $(aElements[0].parentNode).addClass("first");
    
    
    if($("gallerycontent").offsetHeight<700)
    {    
        $("gallerycontent").style.height = "720px";
    }
}

// Shows or hides the flash demo movie- using the function changeHeight
// to scroll the movie into view
function showDemoMovie(oCaller, sImagePath)
{
    var oObject = $("DemoMovie");            
    var isIncrease = (oObject.style.display != "block");
    oObject.iHeight = (isIncrease)? 307 : 0;
    
    if(isIncrease)
    {
        oObject.style.display = "block";
        oCaller.className =  oCaller.className.replace("closed", "open");
        oCaller.innerHTML = oCaller.innerHTML.replace("See", "Close"); //"Close " + sTypeName + " demo";
        
        var so = new SWFObject(sImagePath + "/preview/loader.swf", "contentpreview", "553", "296", "8", "");
        so.addVariable("demo_name", S_DEMO_SWF_PATH);
        so.addVariable("wmode", "transparent");
        so.addVariable("allowScriptAccess", "always");
        so.write("DemoMovie");
        
        if(document.documentElement.scrollTop == 0)
            window.scrollTo(0, 1);
    }
    else
    {
        if(oCaller)
        {
            oCaller.className =  oCaller.className.replace("open", "closed");
            oCaller.innerHTML = oCaller.innerHTML.replace("Close", "See"); 
        }
    }
    if(oCaller)
    {
        var aCorners = $$("#" + oCaller.parentNode.parentNode.parentNode.id + " .corner");
        for(var i=0; i<aCorners.length; i++)
            aCorners[i].style.display = "none";
    }
    
    changeHeight(isIncrease, $$("div.alsoliked"));
}

// Scrolls the demo movie up or down
function changeHeight(isIncrease, oAlsoLiked)
{
    var oObject = $("DemoMovie");
    oObject.iHeight = (isIncrease)? oObject.iHeight + I_DEMO_MOVIE_INCREASE_HEIGHT : oObject.iHeight - I_DEMO_MOVIE_INCREASE_HEIGHT;
    
    if(oObject.iHeight >= 307)
    {
        oObject.iHeight = 307;
        
        demoMovieSlideDone();
    }
    else
    {
        if(oObject.iHeight <= 0)
        {
            oObject.iHeight = 0;
            oObject.style.display = "none";
            $("DemoMovie").innerHTML = "";
            
            demoMovieSlideDone();
        }
        else
        {
            var oFlash = oObject.getElementsByTagName("embed");
            if(oFlash.length > 0)
                oFlash = oFlash[0];
            else
                oFlash = oObject.getElementsByTagName("object")[0];
            oFlash.style.height = oObject.iHeight + "px";
            
            window.setTimeout(function(){ changeHeight(isIncrease, oAlsoLiked); }, I_DEMO_MOVIE_INCREASE_INTERVAL);
        }
    }
    
    oObject.style.height = oObject.iHeight + "px";
}

function demoMovieSlideDone()
{
    $$(".corner").each(function(el){ el.style.display = "block" });
    
    var oThumbs = $$("#gallerycontent .thumbs")[0];
    oThumbs.style.display = "none";
    oThumbs.style.display = "block";
    
    var aPlayButtons = $$("#gallerycontent .notifiers .thumbs li a.thumb .play");
    for(var i=0; i<aPlayButtons.length; i++)
    {
        var oParent = aPlayButtons[i].parentNode;
        var oPlay = aPlayButtons[i].cloneNode(true);
        
        oParent.removeChild(aPlayButtons[i]);
        oParent.appendChild(oPlay);
    }
}

/* Content */
// This function gets called when hovering a rating star in the gallery
// It recursively checks the previous stars as well.
function starRate(oImage, sReplaceWhat, sReplaceWith)
{
    oImage.src = oImage.src.replace(sReplaceWhat, sReplaceWith);
    
    if(oImage.previousSibling != null && 
        oImage.previousSibling.tagName != undefined &&
        oImage.previousSibling.tagName.toLowerCase() == "img")
        starRate(oImage.previousSibling, sReplaceWhat, sReplaceWith);
}

// When a user clicks on a rating star, this function sends an http request
// using /services/GalleryRate.aspx
function rateContent(oImage)
{
    var xmlHttp = new createXMLHttpRequest();
    var sContentId  = oImage.getAttribute("ContentId");
    var sRating     = oImage.getAttribute("rating");
    var sData = "ContentId=" + sContentId + "&Rating=" + sRating + "&seed=" + Math.random();
    
    xmlHttp.open("GET", "/services/GalleryRate.aspx?" + sData, true);
    xmlHttp.onreadystatechange = function()
    {
        if(xmlHttp.readyState == 4)
        {
            var sResponse = xmlHttp.responseText;
            
            if(sResponse == "")
            {
                if($$(".PreviewPage .RatingArea .rateright")[0].innerHTML == "Not Rated Yet")
                {
                    oImage.parentNode.parentNode.parentNode.innerHTML = "Thank You For Voting! The rating will be updated soon.";
                }
                else
                {
                    oImage.parentNode.parentNode.innerHTML = "Thank You For Voting!";
                }
            }
            else
            {
                alert("Problem rating: " + sResponse);
            }
        }
    }
    xmlHttp.send("");
}

// Stops the sound preview by removing the playing elemnt from the page
function soundPreviewStop()
{
    if($("SoundPreviewPlayer"))
        document.body.removeChild($("SoundPreviewPlayer"));
}

// Plays the sound preview according to the browser
// in IE - adding a bgsound element, in FF- adding a quicktime object
function soundPreviewPlay()
{
    var oFlash = $("SoundPreview");
    if(oFlash)
    {
        var oSound;
        
        if (navigator.appName == "Netscape")
        {
            oSound = document.createElement("object");
                oSound.id = "SoundPreviewPlayer";
                oSound.setAttribute("codebase", "http://www.apple.com/qtactivex/qtplugin.cab");
                oSound.innerHTML = "<embed type=\"video/quicktime\" pluginspage=\"http://www.apple.com/quicktime/download\" enablejavascript=\"true\"  src=\"" + S_SOUND_PATH + "\" autoplay=\"true\" loop=\"false\" name=\"SoundPreviewPlayerEmbed\" id=\"SoundPreviewPlayerEmbed\" style=\"width:0; height:0; position: absolute\" />";
        }
        else
        {
            oSound = document.createElement("BGSOUND");
                oSound.src = S_SOUND_PATH;
                oSound.loop = "0";
                oSound.id = "SoundPreviewPlayer";
        }
        
        document.body.appendChild(oSound);
    }
}

// Function for getting a flash object according to browser
// (FF uses the "embed" while IE uses "object"
function getFlashMovieObject(movieName)
{
    if (window.document[movieName]) 
        return window.document[movieName];
        
    if (navigator.appName.indexOf("Microsoft Internet")==-1)
        if (document.embeds && document.embeds[movieName])
            return document.embeds[movieName]; 
    else
        return document.getElementById(movieName);
}

// Checks if the flash is playing by checking the height of the containing element
// If it's less than 50px, displays a message to the user
function checkForFlash(bCheck)
{
    if(!bCheck)
        window.setTimeout(function(){ checkForFlash(true); }, 200);
    else
    {
        var oFlash = $$(".previewflash")[0];
        if(oFlash.offsetHeight < 50)
        {
            //var iWebAd = $$(".txtNoFlashWebAd");
            //if(iWebAd.length > 0)
            //    iWebAd = iWebAd[0].value;
            var iWebAd = 514;
            oFlash.addClass("noflash");
            oFlash.innerHTML = "<p>Your browser is unable to show this flash preview.</p>" +
                                "<p><strong>" +
                                    "Please <a href=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" onclick=\"CountWebAd(" + iWebAd + ");\" target=\"_blank\">click here</a> to update<br />" +
                                    "to the latest Adobe Flash Player." +
                                "<strong></p>";
        }
        else
        {
            var aCorners = $$(".gthumbs .corner");
            for(var i=0; i<aCorners.length; i++)
            {
                aCorners[i].style.display = "none";
                aCorners[i].style.display = "block";
            }
        }
    }
}