﻿
//if (isAppleDevice() == true) {
if (null != null) {
    var videoPositions = new Array('0,100,220,135,1', '130,20,320,180,3', '248,190,160,91,2', '380,10,320,180,8', '745,180,160,91,7', '820,30,240,135,7', '1019,50,180,135,8', '949,139,250,180,9');
    var videoState = new Array('0', '0', '0', '1', '0', '0', '0', '0');
    //var videoSource = new Array('videos/Beco_Short_v3.mov', 'videos/CAP_Short_v2.mov', 'videos/Hennessy_short_v2.mov', 'videos/McCormick_Short_v2.mov', 'videos/thedacare_short_v2.mov', 'href:http://www.reveregroup.com', 'href:http://www.google.com', 'href:http://www.yahoo.com');
    var videoSource = [];
    var imagePath = [];
    var externalUrl = [];
    $j(document).ready(function () {
        // Get data, parse it into an array, and pass array to videoSource.
        $j.ajax({
            url: '/videos.xml',
            type: 'GET',
            dataType: 'xml',
            success: function (xml) {
                $j(xml).find('video').each(function () {
                    //videoSource.push($j(this).attr("jspath"));
                    videoSource.push("/videos/Beco_Short_v3.mov");
                    imagePath.push($j(this).attr("imagePath"));
                    externalUrl.push($j(this).find("externalUrl").text());
                })

            }  // End Success
        }); // End AJAX
    });
    var activePlayerID = '4';
    var playerID;

    window.onerror = silentErrorHandler;

    function silentErrorHandler() {
        return true;
    }

    mw.ready(function () {
        window.setTimeout("animateImage($j('.videoContent_4 img'));", 1000);
    });

    function embedFreshPlayer() {
        var locationHref = videoSource[activePlayerID - 1];
        var url = externalUrl[activePlayerID - 1];
        var image = imagePath[activePlayerID - 1];
        locationHref = locationHref.toString();

        // url redirect
        if (url != '') {
            window.location = url;
            return true;
        }

        mw.load('EmbedPlayer', function () {
            $j('.heroVideoContainer div').embedPlayer({
                'poster': image,
                'height': '270',
                'width': '480',
                'sources': [
                                { 'src': videoSource[activePlayerID - 1] }
                            ]
            })
        });

    }

    function returnImage(videoID) {
        var returnVar = videoPositions[parseInt(videoID) - 1];
        returnVar = returnVar.split(",");

        $j('.videoContent_' + videoID + ' img').parent().animate({
            top: parseInt(returnVar[1]),
            left: parseInt(returnVar[0])
        }, 400);

        $j('.videoContent_' + videoID + ' img').animate({
            height: parseInt(returnVar[3]),
            width: parseInt(returnVar[2])
        }, 400,
	            function () {
	                $j('.videoContent_' + videoID + ' img').parent().css('z-index', parseInt(returnVar[4]));
	            }
	        );
    }

    function animateImage(objRef) {
        var finalTop, finalLeft;

        $j(objRef).parent().css('z-index', '99');

        var newTop = $j(objRef).parent().css('top');
        newTop = newTop.substring(0, newTop.length - 2);

        var newLeft = $j(objRef).parent().css('left');
        newLeft = newLeft.substring(0, newLeft.length - 2);

        var comparisonTop = (parseInt(newTop) + 270);
        var comparisonLeft = (parseInt(newLeft) + 480);

        var offsetTopDiff = ((parseInt(newTop) + 270) - 320);
        var offsetLeftDiff = ((parseInt(newLeft) + 480) - 1200);

        newTop = (newTop - offsetTopDiff) - 35;
        newLeft = (newLeft - offsetLeftDiff) - 35;

        if (parseInt(comparisonLeft) >= 1200 && parseInt(comparisonTop) >= 320) {
            $j(objRef).parent().animate({
                left: parseInt(newLeft),
                top: parseInt(newTop)
            }, 400);
        } else {
            if (parseInt(comparisonTop) >= 320) {
                $j(objRef).parent().animate({
                    top: parseInt(newTop)
                }, 400);
            }
            if (parseInt(comparisonLeft) >= 1200) {
                $j(objRef).parent().animate({
                    left: parseInt(newLeft)
                }, 400);
            }
        }

        $j(objRef).animate({
            height: 270,
            width: 480
        }, 200,
	                function () {
	                    $j(objRef).hide();
	                    $j('.heroVideoContainer').css('top', $j(objRef).parent().position().top - 25);
	                    $j('.heroVideoContainer').css('left', $j(objRef).parent().offset().left);
	                    $j('.heroVideoContainer').css('display', 'block');
	                    $j('.heroVideoContainer').html('<div></div>');
	                    embedFreshPlayer();
	                }
	            );
    }

    $j(document).ready(function () {
        $j(window).load(function () {
            $j('.videoInnerContainer').css('visibility', 'visible');

            $j('.videoInnerContainer div').each(function () {
                if ($j(this).attr('class') != 'heroVideoContainer') {
                    var origTop = $j(this).css('top');
                    $j(this).css('top', '330px');
                    $j(this).animate({
                        top: origTop,
                        opacity: '1.0'
                    }, 600);
                }
            });
        });

        $j('.videoInnerContainer img').click(function () {
            var videoVar;
            var prevVideoID = activePlayerID;

            $j('.heroVideoContainer').css('display', 'none');

            playerID = $j(this).parent().attr('class');
            playerID = playerID.split('_');
            activePlayerID = playerID[1];

            videoVar = videoState[parseInt(activePlayerID) - 1];

            if (parseInt(prevVideoID) != parseInt(activePlayerID)) {
                returnImage(prevVideoID);
                videoState[parseInt(prevVideoID) - 1] = 0;
            }

            if (parseInt(videoVar) == 0) {
                videoState[parseInt(activePlayerID) - 1] = 1;
                animateImage($j(this));
            } else {
                videoState[parseInt(activePlayerID) - 1] = 0;
                returnImage(activePlayerID);
            }
        });

        $jParallax('.videoInnerContainer').jparallax();

        $j('.video-holder').css('margin', '0px');
        $j('.content-holder').css('margin', 'auto');
        $j('#main h1').css('text-align', 'center');
    });
}
if (runFlash() == false) {
    $j(document).ready(function () {
        $j('.video-holder').css('height', '5px');
    });
}
else {
    $j(document).ready(function () {
        var objHTML = '<object width="1200" height="320">';
        objHTML += '<param name="wmode" value="transparent">';
        objHTML += '<param name="movie" value="/Assets/flash/Main.swf">';
        objHTML += '<param name="allowscriptaccess" value="always"></param>';
        objHTML += '<embed src="/Assets/flash/Main.swf" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="transparent" width="1200" height="360"></embed>';
        objHTML += '</object>';

        $j('.video-holder').html(objHTML);
    });
}

