﻿var videoPlayer = "#youtubePlayer";
var payAd = "View TV Ad"
var closeAd = "Close TV Ad"
var onEpitProduct = false;
var scrollspeed = 800;

// Document ready handler
$(document).ready(function () {

    var videostrips = $(".videoStrip");
    var videoitems = $(".videoItem");

    var prod = $("#imageInfoControl").length
    var epit = $("#pitarea").length

    // true if on product page
    if (prod > 0) {
        if (videostrips.length == 2 && videoitems.length == 2) {
            $(".videoStrip").hide();
        }

        var video = GetFirstVideo();
        $(videoPlayer).attr("video", video);
    }
    else if (epit > 0) {

        if (videostrips.length == 1 && videoitems.length == 1) {
            $(".videoStrip").hide();
        }

        var video = GetFirstVideo();
        $(videoPlayer).attr("video", video);
    }

    var obj = $("#youtubePlayer2");
    if (obj.length > 0) {
        videoPlayer = "#youtubePlayer2";
        onEpitProduct = true;
        SetTVimageVideoHandler();
        var video = GetFirstVideo();
        $(videoPlayer).attr("video", video);
    }

    InitVideoPlayer();

    if ($("#videoTab").length) {
        $("#videoTab a").live("click", function (e) {
            $("#videoTab li").removeClass("selected")
            $(this).parents("li").addClass("selected");
            var tab = $(this).attr("tab")
            SetVideoTabHandler(e, tab);
        });
    }
    SetVideoPlayHandler();
    SetVideoProductPageBtnHandler();

    // init slider
    if ($(".scrollable").length) {
        $(".scrollable").scrollable();
    }
});

function scrollprev(i) {
    var obj = $(".scrollable")
    var api = obj.eq(i).data("scrollable");
    api.prev(scrollspeed);
    return false;
}

function scrollnext(i) {
    var obj = $(".scrollable")
    var api = obj.eq(i).data("scrollable");
    api.next(800);
    return false;
}

$(".scrollright").live("mouseover", function (e) {    
    row = $(this).attr("row");    
    scrollnext(row);
 });

$(".scrollleft").live("mouseover", function (e) {    
    row = $(this).attr("row");    
    scrollprev(row);
});


// =====================================================================================
// Functions
// sets video handler to video thumbnail
function SetVideoProductPageBtnHandler() {

    var obj = $("#viewAdbtn");

    if (obj.length > 0) {

        $('#tvbtn, #viewAdbtn').click(function (e) {

            e.preventDefault();
                        
            var video = GetFirstVideo();                        
            $(videoPlayer).attr("video", video);

            var btnText = $("#viewAdbtn").html();
            if (btnText == payAd) {
                SetElements();
            }
            else if (btnText == closeAd) {
                $(videoPlayer).tubeplayer('stop');
                $("#prodvidwrapper").hide();
                $("#epitvidwrapper").hide();                
                $("#mainProductImage").show();
                $(videoPlayer).hide();
                $("#viewAdbtn").html(payAd);
                $("#enlargeBut").show();
            }
        });       
    }  
}

function SetElements() {

    if ($("#epitvidwrapper").length > 0) {
        $("#epitvidwrapper").show();
    }

    if ($("#prodvidwrapper").length > 0) {
        $("#prodvidwrapper").show();
    }      
    
    $(videoPlayer).show();
    PlayVideo();
    $("#mainProductImage").hide();          
    $("#viewAdbtn").html(closeAd);
    $("#enlargeBut").hide();
}

function PlayVideo() {
    InitVideoPlayer();    
    var video = $(videoPlayer).attr("video");
    setTimeout(function () { $(videoPlayer).tubeplayer('play', video); }, 700);
    window.location = '#toppage';
}

// sets video handler to video thumbnail
function SetVideoPlayHandler() {

    var obj = $(".videoItem");

    if (obj.length > 0) {

        $('.videoItem .clip').click(function (e) {

            e.preventDefault();

            $('.videoItem .clip').removeClass('selected')
            $(this).addClass('selected');

            //set video
            var video = $(this).attr('video');
            $(videoPlayer).attr("video", video);

            if ($("#viewAdbtn").length > 0) {

                var btnText = $("#viewAdbtn").html();

                if (btnText == payAd) {
                    SetElements();
                }
                else if (btnText == closeAd) {
                    PlayVideo();
                }
            }
            else {

                if (onEpitProduct) {
                    $('#tvplay').hide();
                    $('.magnifier').hide(); 
                    $('#tvClose').show();                    
                    $("#epitvidwrapper").show();
                    $(".product_image").hide();
                    $(videoPlayer).show();
                    PlayVideo();
                }
                else {
                    PlayVideo();
                }
            }
        });

        $('.videoItem .clip').hover(function (e) {
            $(this).css("cursor", "pointer");
        });
    }
}


function SetTVimageVideoHandler() {

    $('#tvplay').click(function (e) {
        e.preventDefault();

        $(this).hide();
        $('.magnifier').hide(); 
        $('#tvClose').show();

        var video = GetFirstVideo();
        $(videoPlayer).attr("video", video);

        $('.videoItem .clip').removeClass('selected')
        $('.videoItem .clip').eq(0).addClass('selected');
        $("#epitvidwrapper").show();
        $(".product_image").hide();
        $(videoPlayer).show();
        PlayVideo();
    });

    $('#tvClose').click(function (e) {
        e.preventDefault();
        $('.magnifier').show(); 
        $(this).hide();
        $('.videoItem .clip').removeClass('selected')
        $('#tvplay').show();
        $(videoPlayer).tubeplayer('stop');
        $(videoPlayer).hide();        
        $("#epitvidwrapper").hide();
        $(".product_image").show();        
        $("#enlargeBut").show();
    });
}

function SetVideoTabHandler(e, tab) {

    e.stopPropagation();
    e.preventDefault();      
        
    $.ajax({
        url: "GetVideosByTabOption.ajax",
        data: ({
            tab: tab
        }),
        cache: false,
        type: "GET",
        success: function (res) {

            var y = res.split("||");

            if (y[0] == "OK") {

                if (y.length == 2) {

                    var results = y[1];

                    if (results.length > 1) {
                        $("#videolist").html(results);                        
                        SetVideoPlayHandler();
                        $(".scrollable").scrollable();
                    }
                    else {
                        $("#videolist").html("");
                    }
                }
                else {
                    $("#videolist").html("");
                }
            }
        }
    });
    return false;
}

function GetFirstVideo() {
    var videolist = $(".videoItem").eq(0);
    var video = $(".clip", videolist).attr("video");
    return video;
}

// initializes video player
function InitVideoPlayer() {

    var obj = $(videoPlayer)
    if (obj.length == 0) return;

    var featuredVideo = $(videoPlayer).attr("video");
    var vWidth = $(videoPlayer).attr("vw");
    var vHeight = $(videoPlayer).attr("vh");

    $(videoPlayer).tubeplayer({       
        width: vWidth + "px", // the width of the player
        height: vHeight + "px", // the height of the player
	    allowFullScreen: "true", // true by default, allow user to go full screen
	    initialVideo: featuredVideo, // the video that is loaded into the player
	    start: 0, 
	    preferredQuality: "hd720",// preferred quality: default, small, medium, large, hd720
	    showControls: 1, // whether the player should have the controls visible, 0 or 1
	    showRelated: 0, // show the related videos when the player ends, 0 or 1 
	    autoPlay: 0, // whether the player should autoplay the video, 0 or 1
	    autohide: 1,      	
	    color: "red", // possible options: "red" or "white"
	    showinfo: false, // if you want the player to include details about the video
	    modestbranding: true, // specify to include/exclude the YouTube watermark
	    // the location to the swfobject import for the flash player, default to Google's CDN
	    wmode: "transparent", // note: transparent maintains z-index, but disables GPU acceleration
	    swfobjectURL: "http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js",
	    loadSWFObject: true, // if you include swfobject, set to false
	    // HTML5 specific attrs
	    iframed: true, // iframed can be: true, false; if true, but not supported, degrades to flash
	    // Player Trigger Specific Functionality
	    onPlay: function(id){}, // after the play method is called
	    onPause: function(){}, // after the pause method is called
	    onStop: function(){}, // after the player is stopped
	    onSeek: function(time){}, // after the video has been seeked to a defined point
	    onMute: function(){}, // after the player is muted
	    onUnMute: function(){}, // after the player is unmuted
	    // Player State Change Specific Functionality
	    onPlayerUnstarted: function(){}, // when the player returns a state of unstarted
	    onPlayerEnded: function(){}, // when the player returns a state of ended
	    onPlayerPlaying: function(){}, //when the player returns a state of playing
	    onPlayerPaused: function(){}, // when the player returns a state of paused
	    onPlayerBuffering: function(){}, // when the player returns a state of buffering
	    onPlayerCued: function(){}, // when the player returns a state of cued
	    onQualityChange: function(quality){}, // a function callback for when the quality of a video is determined
	    // Error State Specific Functionality
	    onErrorNotFound: function(){}, // if a video cant be found
	    onErrorNotEmbeddable: function(){}, // if a video isnt embeddable
	    onErrorInvalidParameter: function(){} // if we've got an invalid param
    });
}

