// when a category tab is clicked load the contents via ajax
 $(document).ready(function(){

   $('#vid-search-keyword').focus(function(){
     if ($(this).val()=='keyword')
       $(this).val('');
                          }).blur(function(){
     if ($(this).val()=='')
       $(this).val('keyword');
                          });


   //hide the share link
   $('#sharelink').css({position:'absolute',right: '-400px',bottom:"5px"});


   $('#shareiconurl').live('click',function(){
     if ( $('#sharelink').css('right')=='-400px' ) {
       $('#sharelink').animate({right:"35px"},{ queue:false, duration:500 });
     }else {
       $('#sharelink').animate({right:"-400px"},{ queue:false, duration:500 });
     }
     return false;
   });

   $('#shareurl').focus(function(){
     $(this).select();
     if( window.clipboardData && clipboardData.setData ) {
		  clipboardData.setData("Text", $(this).val());
	   }

   })

   //bindable function
   $('a[class="catvids"]').bind('click',showVidsByCategory);

   $('a.onevid').bind('click',showVideo);

   $('#vidsearchtab').bind('click',clickSearchTab) ;

   $('#vidsearchbutton').bind('click',vidSearch);

 });



//Bound Functions

function showVidsByCategory() {

   //Click on a category Tab

   $(this).parent().siblings().removeClass('current');
   $(this).parent().addClass('current');

     //make sure the search box is closed
     $('#vidbox-search').slideUp();

     // get the target url and amend it so we know it's an ajax call
     var sTarget = $(this).attr('href');
     sTarget = sTarget+'/ajx/true';

     //put up a loading animation
     $('#vidbox-vids').html('<div id="vidbox-loading" style="width:210px;height:15px;margin:150px auto;"><img src="/img/loadingAnimation.gif" width="208" height="13" alt="{animation:loading}"/></div>');

     //get the video list
     $.getJSON(sTarget,'',function(data){
       //populate the videos into the panel
       $('#vidbox-vids').html(data['vidboxVids']);
       //$('#topvideospanel').html(data['topVids']);
       //$('#related-vids').html(data['relatedVids']);
       binder();
     });


     // unbind and bind all function Bind



     return false;

}


//what about a single video
function showVideo() {


     //scroll to top of screen
     //get y pos of #content-area
     var aPosCA = $('#content-area').offset()
     var yPos = (aPosCA.top) ? aPosCA.top : 262 ;
     window.scrollTo(0,yPos);

     //put up a loading animation
      $('#one-large-video-source').html('');
      $('#one-large-video').html('<div id="one-large-vid-loading" style="width:210px;height:15px;margin:150px auto;"><img src="/img/loadingAnimation.gif" width="208" height="13" alt="{animation:loading}"/></div>');
      $('#one-large-video-details').html('<div id="one-large-vid-details" style="width:210px;height:15px;margin:auto;"><img src="/img/loadingAnimation.gif" width="208" height="13" alt="{animation:loading}"/></div>');
      $('#topvideospanel').html('<div id="top-videos-panel" style="width:210px;height:15px;margin:190px auto;"><img src="/img/loadingAnimation.gif" width="208" height="13" alt="{animation:loading}"/></div>');


       var sVidTarget = $(this).attr('href') ;
       //remove any trailing slash and add on our ajax marker
       sVidTarget = sVidTarget.replace(new RegExp(/\/$/),'');
       sVidTarget = sVidTarget+='/ajx/true' ;

       //get the individual video AND the related video list
      $.getJSON(sVidTarget,'',function(data){
       //populate the videos into the panel


         var sHtml = '<a href="'+data['onevid']['url']+'" ';
         sHtml=sHtml+ 'class="media" ';
         sHtml=sHtml+ '>'+data['onevid']['title']+'</a>';
         sHtml=sHtml+ '<div id="one-large-video-source" class="video-source">'+data['onevid']['source_logo']+'</div>';

         sHtml=sHtml+'<div id="sharebox">';
         sHtml=sHtml+'<div id="sbc">';
         sHtml=sHtml+'<div id="sharelink">';
         sHtml=sHtml+'  <label for="shareurl">Link to this page:</label>';
         sHtml=sHtml+'  <input type="text" size="27" value="http://www.bizstartup.ie/video/v/'+data['onevid']['id']+'" id="shareurl" />';
         sHtml=sHtml+'</div>';
         sHtml=sHtml+'<a href="http://www.bizstartup.ie/video/v/'+data['onevid']['id']+'" id="shareiconurl" title="share this video"><img src="/img/share-icon.gif" alt="share" width="22" height="25" /></a>';
         sHtml=sHtml+'</div>';
         sHtml=sHtml+'</div>';

//alert(sHtml)
         $('#one-large-video').html(sHtml)

         $('#sharelink').css({position:'absolute',right: '-400px',bottom:"5px"});

         //jquery media needs activated on this replaced link

        //sHtml=sHtml+ }} ';
         $('#one-large-video>a.media').media(
                                              {  width:480,
                                                 height:368,
                                                 bgColor:'#000000',
                                                 params:{allowfullscreen:true},
                                                 flashvars:{allowscriptaccess: 'always',
                                                            usefullscreen:true,
                                                            autostart: false,
                                                            backcolor:'001001001',
                                                            frontcolor:'900900000',
                                                            image: data['onevid']['oImg']['url'],
                                                            logo:data['onevid']['source_logo']['source_url']
                                                            }
                                                }
                                             );

         //now the details for the video
         var sDetailsHtml = '<h2>'+data['onevid']['title']+'</h2>'+data['onevid']['summary']+'' ;
         $('#one-large-video-details').html(sDetailsHtml) ;

         $('#one-large-video-source').html(data['onevid']['source_logo']);

         //now the related videos
         $('#topvideospanel').html(data['relatedVids']);

         binder();
      });


      return false;
}


function clickSearchTab() {

     //toggle the tabs styles
     $(this).parent().siblings().removeClass('current');
     $(this).parent().addClass('current');

     //put up a loading animation
     $('#vidbox-vids').html('');
     //slide the search form open
     $('#vidbox-search').slideDown();

     binder();
}

function vidSearch() {


      //put up a loading animation
      $('#vidbox-search-vids').html('<div id="vidbox-loading" style="width:210px;height:15px;margin:150px auto;"><img src="/img/loadingAnimation.gif" width="208" height="13" alt="{animation:loading}"/></div>');

      var vskValue = $('input[name="vsk"]').val();
      var vscValue = $('select[name="vsc"]').val();
      var sFormTarget = '/video/ajx/true/';

      $('#vidbox-search-vids').html('');

      //get the video list
      $.getJSON(sFormTarget,{vsk:vskValue,vsc:vscValue},function(data){
        //populate the videos into the panel
        //$('#vidbox-vids').html(data['vidboxVids']);
        $('#vidbox-search-vids').html(data['vidboxVids']);
        //$('#vidbox-vids').fadeIn();
        //$('#vidbox-search').hide();

        binder();
      });

      return false;



}


function binder() {
     $('a[class="catvids"]').unbind('click',showVidsByCategory);
     $('a[class="catvids"]').bind('click',showVidsByCategory);

     $('a.onevid').unbind('click',showVideo);
     $('a.onevid').bind('click',showVideo);

     $('#vidsearchtab').unbind('click',clickSearchTab);
     $('#vidsearchtab').bind('click',clickSearchTab);

     $('#vidsearchbutton').unbind('click',vidSearch);
     $('#vidsearchbutton').bind('click',vidSearch);
}

