﻿/*
 * Authors:
 * - Anthony Steele, Naeem Khedarun, Ben Dwyer 
 *
 * Functions to swap static image with Flash video on mouse in, and back to the image on mouse out
 * depends upon: jQuery, jQuery.plugins.hoverintent, SwfObject 
 */

// counters to keep track of image in/out state
var divCount = 1;
var lastImageIn = null;

/*
 * Dummy empty function 
 */
voidFunction = function() {};
    
/*
 * Hide the flash div. Call on the parent ImageDiv 
 */
hideFlash = function(parentDiv)
{
    var flashDivs = $(parentDiv).find("#mainFlashPlayer");
    
    // reverse the mouse in - hide the flash, show the image
    $(flashDivs).hide();

	/*scrapped
    var imgDiv = getImageDiv($(parentDiv))
    imgDiv.show().css("opacity", "1");
    */
    
    // remove the flash - use a delay to avoid IE showing black
    setTimeout( function() { $(flashDivs).remove(); lastImageIn = null; }, 50);
}

/*
 *  Handles the MouseOut event. if needed, hide the flash
 */
imageDivMouseOut = function() {
    hideFlash(this);
};

/*
 * Gets the image div depending on whether its a root category page
 * or if it has been refined.
 */
getImageDiv = function(parent) {
    
    // check that there is a video source
    var imgNonRefiningDiv = parent.find("img.productImage");
    var imgRefiningDiv = parent.find("img.product-image");

    if (imgNonRefiningDiv[0] != null) {
        return imgNonRefiningDiv;
    } else {
        return imgRefiningDiv;
    }
}

/*
*   Handles the MouseIn event. Hides the product image and shows the flash video.
*/
imageDivMouseIn = function() {
	//This is here until this bug is fixed: http://bugs.adobe.com/jira/browse/FP-240
	var docTitle = document.title;
	
    // force exit of the last item showing flash video
    if ((lastImageIn != null) && (lastImageIn != this)) {
        hideFlash(lastImageIn);
    }

    lastImageIn = this;

    var imgDiv = getImageDiv($(this))[0];
    
    var videoSrc = $(imgDiv).attr("videosrc");

    var hasFlashPlayer = swfobject.hasFlashPlayerVersion("9.0.0");

    // cannot continue if the videoSrc is missing or the correct flash player
    // is not installed.
    if (!videoSrc || !hasFlashPlayer) {
        return;
    }

    // create a div to host the flashc
    var hiddenDivId = "hiddenFlashDiv" + divCount++;
    $(this).append("<div id=\"" + hiddenDivId + "\"> </div>");
    var hiddenDiv = $(this).find("#" + hiddenDivId)[0];

    //create flash 
    var flashvars = {
        videoURL: videoSrc,
        clickURL: $(imgDiv)[0].parentNode.href
    };
    var params = {
        menu: "false",
        scale: "scale",
        allowFullscreen: "true",
        allowScriptAccess: "always",
        bgcolor: "#FFFFFF",
        wmode: "transparent"
    };
    var attributes = {
        id: "mainFlashPlayer"
    };

    // show flash
    swfobject.embedSWF("/assets/AsosCom/flash/AsosFlashPlayer.swf", hiddenDivId, "180", "230", "9.0.0", "expressInstall.swf", flashvars, params, attributes);

    // now hide the image 
    /* actually scrap that
    $(imgDiv).animate({
        opacity: 0
    },1000);*/
    $(this).find("object").css({
        "position": "absolute",
        "left": "0px",
        "top": "0px"
    });
    
	//This is here until this bug is fixed: http://bugs.adobe.com/jira/browse/FP-240
	document.title = docTitle;
};

/*
 * Attaches the miniclip mouse over routines to category images (this uses the livequery plugin which means we don't have to keep calling it).
 */
$(document).ready(function() {
    var hoverConfig = {
        sensitivity: 6, // number = sensitivity threshold (must be 1 or higher)    
        interval: 0, // number = milliseconds for onMouseOver polling interval    
        over: imageDivMouseIn, // function = onMouseOver callback (REQUIRED)    
        timeout: 200, // number = milliseconds delay before onMouseOut    
        out: imageDivMouseOut // function = onMouseOut callback (REQUIRED)    
    };
    $("div.categoryImageDiv").livequery(function() {
		$(this).hoverIntent(hoverConfig);
	});
});