/* landing site script */

$(document).ready( function(){
      
   /* news page */
   $('.old').click( function(e){
        /* clicked news id */
        id = $(e.target).attr('id');
        $('#text_'+id).animate({"height": "toggle"}, "fast")
    });
   
   /*  */
   var log = false;
   /* video url */
   url = $('#dealer_url').val();
   /* clip file id */
   fid = $('#clip_fid').val();
   /* if player exists */
   if( $('#player').length )
   {
       id= setInterval( function(){
            state = jwplayer('player').getState();
            
            if( 'PAUSED' == state && log && url && fid )
            {
                log = false;
                /* see annab popup blokeerimise teate */
                //window.open('http://'+url, 'mywindow','width=400,height=200,left=0,top=100,screenX=0,screenY=100');
               	//parent.location='http://'+url;
                data = 'click_count=1&dealer_url='+url+'&fid='+fid;
                ajax_req( '0', data );
            }
            else if( 'PLAYING' == state )
            {
                log = true;
            }
            else
            {
                log=false;
            }
       }, 500 );
   }
   
   /* video player - see osa ei töötand IE-ga*/
   /*$('#video_area').click(function(e){
    
    state = jwplayer('player').getState();
        alert(state);
    if( $(e.target).is('#player') )
    {
        state = jwplayer('player').getState();
        alert(state);
        url = $('#dealer_url').val();
        fid = $('#clip_fid').val();
        
        //console.log(state);
        /* if current status is paused, url and video id is defined then open new window and increase click count by ajax request */
       /* if( ('PAUSED' == state ) && url && fid )
        {        
            window.open('http://'+url);
            //console.log(url+' '+old_state);
        
            data = 'click_count=1&dealer_url='+url+'&fid='+fid;
            ajax_req( '0', data );
        }
    }
    
   });*/
   
   /* video list pagination params */
   if( getPageNumber() )
   {    
        getListContent(0);
   }
   
   $('#list').live('click', function(e){
    
        if( $(e.target).is('.clips_pages a') )
        {
          setTimeout( function(){
            getListContent(0);
          }, 500);  
        } 
   });
   
   $('#clip_cnt').live( 'change', function(){
        getListContent(1);
   });
   
   
   /* get page number from url */
   function getPageNumber()
   {
        if( window.location.href.indexOf('#page')+1 ){
            return window.location.href.slice(window.location.href.indexOf('#page') + 1).split('=');
        }
        return new Array('page', '1');
   }
   
   /* get clip cnt number from url */
   function getClipCnt()
   {
        if( window.location.href.indexOf('&clip_cnt')+1 ){
            return window.location.href.slice(window.location.href.indexOf('&clip_cnt') + 1).split('=');
        }
        //return new Array('clip_cnt', '1');
        return 0;
   }
   
   /* get new clip list content  */
   function getListContent( type )
   {
      $('.clips-list').html('Loading..');
      var url = getPageNumber();
      
      var dealer_src = $('#dealer_source').val();
      var order_type = $('#order_type').val();
      var clip_id = $('#clip_id').val();
      /* clip count on page */
      if( 1 == type ){
        var clip_cnt = $('#clip_cnt').val();
      }
      else{
        var clip_cnt = $('#clip_cnt').val();
        var _clip_cnt = getClipCnt(); 
        if( _clip_cnt ){
            clip_cnt = _clip_cnt[1];
        } 
      }
      
      /* get new content for clip list */
      data = 'list_content=1&dealer_src='+dealer_src+'&page='+url[1]+'&clip_id='+clip_id+'&order_type='+order_type+'&clip_cnt='+clip_cnt;
      //console.log(data);
      ajax_req( 'list', data );
   }
   
   /* ajax request
       @param id - div id name, that id html content changed
       @param data - POST data to send
    */
    function ajax_req( id, data )
    {
        $.ajax({
            type: "POST", 
            url:'/ajax_req.php',
            data: data,
            success: function(html){
                
                if( id && html.length )/* if id defined then change defined id html content */
                {
                 $('#'+id).html(html); 
                 
                 /* get focus on right place */
                 $('.sel_clip a').focus();  
                }
            }
        });
    }
  
  
  
});

