//IM SO LATE

$(function()
{
    jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
    };

$.fn.nav= function()
{
    var $this = $(this);
    var timeout    = 500;
    var closetimer = 0;
    var ddmenuitem = 0;

    $('.subnav', $this).hide();

    $( '.collapse', $this ).live('click',function() {
        $(this).next('ul').slideUp();
        $(this).removeClass('collapse').addClass('expand');
    }); 

     $( '.expand', $this ).live('click',function() {
        $( '.collapse', $this ).removeClass('collapse').addClass('expand').next('ul').slideUp();
        $(this).next('ul').slideDown();
        $(this).removeClass('expand').addClass('collapse');
     });

}
});

$(document).ready(function(){

 var videoId = $('#bgPlayer').attr('title');
 var player;
 var embedOptions = {
    src:"/system/application/views/themes/56factory/swf/BgPlayer.swf",
    wmode : 'transparent'
 };

 var flashVars = {
    videoURL : '/assets/'+videoId+'.flv'
 };


// flashembed('#bgPlayer', embedOptions, flashVars);
var bgImg= $(".bgImg");


 window.onresize = centerAndResize

 function centerAndResize()
 {
      
    var orig_h = bgImg.eq(0).height();
    var new_w =  bgImg.width() * (  $(window).height() / orig_h );

    bgImg.height($(window).height());
    bgImg.width ( new_w );
  
    bgImg.css ('left', $(window).width()/2 -  bgImg.width()/2 );
 }



 $('#main_nav').nav();

 //$('#v').width($('.thumb').size()*130);

 $('#videothumbs').serialScroll({
		items:'li.thumb',
                exclude :7,
		//offset:-140, //when scrolling to photo, stop 230 before reaching it (from the left)
		start: 0, //as we are centering it, start at the 2nd
		duration:1200,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		easing:'easeOutQuart', //use this easing equation for a funny effect
		jump: false 
});

$('#prev').click(function(){$('#videothumbs').trigger('prev')});
$('#next').click(function(){$('#videothumbs').trigger('next')});

        //var bgPlayer = $('#bgPlayer object').get(0);

        var lastClicked;
        var currentIndex;
        var thumbs = $('#videothumbs a');
        var thumbs_n = thumbs.size();
        var next;


        function flowplayer(videoId, onload){


           if (typeof onload == 'undefined')
           {
            onload = function(){};
           }

           player = $f("player", "/swf/flowplayer-3.2.5.swf", {

             onLoad : onload,

             clip: {
                url: '/video/index.php?file='+videoId+'.flv',
                autoPlay: true,
                provider:'lighttpd',
                scaling : 'fit',
                onMetaData : function(){
                  
                  var clip = this.getClip();

                  var orig_h = clip.height;
                  var orig_w = clip.width;

                  var height = $(window).height()-80;

                  if(height > 640) height = 640;

                  var width = orig_w * (  height / orig_h );

                  var modalContainer =$('#simplemodal-container');

                  
                 modalContainer.find('object').css({width: width, height: height});
                 modalContainer.css({width : width, height: height});

//                  width=modalContainer.find('#playerContainer').width();
//                  height = modalContainer.find('#playerContainer').height();
                  var left = $(window).width()/2 - width/2;
                  modalContainer.css('left',left).width(width);
                  modalContainer.css('top', ($(window).height()/2-height/2) );

            
                }

             },

             canvas: {
                backgroundGradient: "none",
                backgroundColor: "#000000"
             },

             plugins: {

                controls: {
                    bottom: 0,
                    opacity: 1,
                    autoHide : 'fullscreen',
                    backgroundGradient: 'none',

                    progressColor: '#c9c609',
                    all: false,
                    play: true,
                    scrubber: true,
                    volume: true,
                    fullscreen : true

                },

                lighttpd: {
                    url: '/swf/flowplayer.pseudostreaming-3.2.5.swf',
                    queryString:escape('&start=${start}')
                 },

                viral: {
                    url: '/swf/flowplayer.viralvideos-3.2.3.swf',
                    share: {
                            description: ""
                    }
                }
             }
            });
        }

        function updateModal(){
            var modalContainer =$('#simplemodal-container');

            if(lastClicked.hasClass('openImage'))
            {
                var imgURL = '/assets/'+lastClicked.attr('id')+'.jpg';
                var cacheImage = document.createElement('img');
                cacheImage.src = imgURL;

                $(cacheImage).load(function()
                {
                  
                  var resize='';
                  var maxH = $(window).height()-50;
                  var maxW = $(window).width()-200;
                  var width = cacheImage.width;

                  resize= 'height="'+maxH+'"';
                  width = width * (  maxH / cacheImage.height );

                

                  if(width > maxW)
                  {
                     
                      resize = 'width="'+maxW+'"';
                      maxH = maxH * (maxW / width);
                      width = maxW;
                  }

                  var html='<div id="imgContainer"><a class="modalPrev"></a><a class="modalNext"></a>'+
                    '<img id="img" '+resize+' src="'+imgURL+'" />'+
                    '</div>';

                  var left = $(window).width()/2 - width/2;
                  
                  modalContainer.css('left',left).width(width).height(maxH+20);
                  modalContainer.css('top', $(window).height()/2-maxH/2 );
                  modalContainer.find('#simplemodal-data').html(html);


                });
            }
            else if(lastClicked.hasClass('playVideo'))
            {
           
               var html = '<div id="playerContainer">'+
                '<div id="playerObject">'+
                '<a id="player"></a></div><a class="modalPrev"></a><a class="modalNext"></a>"33';

               modalContainer.find('#simplemodal-data').html(html);
               
               
               flowplayer(lastClicked.attr('rel'),
                    function(){
                           var width=modalContainer.find('#playerContainer').width();
                           var height = modalContainer.find('#playerContainer').height();
                           var left = $(window).width()/2 - width/2;
                           modalContainer.css('left',left).width(width);
                           modalContainer.css('top', $(window).height()/2-height/2 );
                    }
               );


            
            }

         
        }



        $('.modalPrev').live('click',function(){

           if(currentIndex == 0)
               currentIndex = thumbs_n-1;
           else
               currentIndex--;

           lastClicked = thumbs.eq(currentIndex);


           updateModal();

        });

        $('.modalNext').live('click',function(){
            if(currentIndex == thumbs_n -1)
               currentIndex = 0;
           else
               currentIndex++;

          

           lastClicked = thumbs.eq(currentIndex);
           updateModal();
        });



        $('.openImage').click(function(){

             lastClicked = $(this);
             currentIndex = thumbs.index(lastClicked);

             var imgURL = '/assets/'+$(this).attr('id')+'.jpg';

             var cacheImage = document.createElement('img');
             cacheImage.src = imgURL;

             $(cacheImage).load(function(){

                  var height='';
                  var maxH = $(window).height()-50;

                  if(cacheImage.height > maxH)
                      height= 'height="'+maxH+'"';
                  
                  $.modal('<div id="imgContainer"><a class="modalPrev"></a><a class="modalNext"></a>'+
                    '<img '+height+' src="'+imgURL+'" />'+
                    '</div>',
                    {
                       opacity:70,
                       overlayCss: {backgroundColor:"#000"},
                       closeHTML : '<a id="simplemodal-close"> CLOSED</a>'
                    }
                  );
             });

             return false;
        });


        $('.playVideo').click(function(){

           // bgPlayer.pause();
            lastClicked = $(this);
             currentIndex = thumbs.index(lastClicked);

            $.modal('<div id="playerContainer">'+
                ''+
                '<div id="playerObject">'+
                '<a id="player"></a></div><a class="modalPrev"></a><a class="modalNext"></a>"',
               {
                   opacity:70,
                   overlayCss: {backgroundColor:"#000"},
                   closeHTML : '<a id="simplemodal-close"> CLOSED</a>',
                   onClose: function () {
                       $f().stop();
                     //  bgPlayer.resume();
                       $.modal.close();
                   }
               }
            );

           flowplayer($(this).attr('rel'));

           return false;
        });

               // preload e rollover immagini
               var cache = [];
               $("img.rollover").each (function(){

               var cacheImage = new Image();
               cacheImage.src = this.src.replace("_off","_on");

               cache.push(cacheImage);

               $(this).hover(

                  function()
                  {
                    this.src = this.src.replace("_off","_on");
                  },

                  function()
                  {
                    this.src = this.src.replace("_on","_off");
                  }
               );
          });

 bgImg.load(function(){centerAndResize();bgImg.show()})
});


