$(document).ready(init_portfolio);
var selected_item = null;
var current_button = null;
var screen_click = false;
var s_height;
var player; 
function init_portfolio(){
  $(".portfolio_items>li>a").bind("click", 
    function(e){

      // get content
      selected_item = this;
      selected_index = $(".portfolio_items>li>a").index(this);
      set_cookie('last_portfolio', selected_index);

      if(player!=undefined){
         player.sendEvent('STOP');
         player = undefined;
      }
                
      $(".portfolio_items>li>a").removeClass("selected");
      $(this).addClass("selected");
      $.get(this +'?ref=portfolio',
        function(data){
          
          $("#content_left").html(data); 
          screen_click = false;
          $(".content_menu>li>a").bind("click",
            function(e){
              if(s_height!=undefined){
                $('#content_screen').css('height', s_height);  
              }
              
              $("#content_desc").fadeOut('slow',function(){
                //alert("HERE");
                $("#content_desc").html($(current_button).attr("title"));
              });
              
              $("#content_desc").fadeIn('slow');
              
              $(".content_menu>li>a").removeClass("selected");
              $(this).addClass("selected");
              current_button = this;
              
              if($(this).attr('url')==''){
                $('#go_url').css({display:'none'});
              }else{
                $('#go_url').attr('href', $(this).attr('url'));
                $('#go_url').attr('target', '_blank');
                                              
                $('#go_url').css({display:'block'});
              }
              show_content(  selected_item + $(this).attr("content"), $(this).attr("screen_height"));
              return false;
            }
          );
          
          $("#nav_next").bind("click",click_show_next);
          $("#nav_prev").bind("click",click_show_prev);   
          

          $('#nav_more').bind("click",toogle_longdesc);     
          $('#longdesc_close').bind("click",toogle_longdesc);

          first_run = true;
          $(".content_menu>li>a:first").trigger('click');
          setTimeout(function(){$('#nav_more').trigger("click");}, 1000);
          
        }
      );
      
      return false;
    }
  )

  $("#loading").ajaxStart(
    function(){
      $(this).show();
    }
  )
  $("#loading").ajaxComplete(
    function(){
      $(this).hide();
    }
  )
  
  
  if(non_portfolio){
    //$(".portfolio_items>li>a:first").trigger('click');
    
    portfolio_items = $('.portfolio_items>li>a');
    
    last_portfolio = get_cookie('last_portfolio');
    
    if(last_portfolio == null || last_portfolio==undefined)last_portfolio = 0;

    
    $(portfolio_items[last_portfolio]).trigger('click');
    
    // alert($(portfolio_items[portfolio_items.length-1]).trigger('click'));
    
  }
 
}
function toogle_longdesc(){

  if($('#content_longdesc').css('display') =='none'){
    $('#nav_more').addClass('selected');
    $('#content_longdesc').css('display','block'); 
    $('#content_longdesc_pan').animate({left:0},500, undefined,
       function(){
        $('#nav_more span').html('Read less');
       }
    );
  }else{
    $('#content_longdesc_pan').animate({left:-460},500,undefined,
        function(){
          // Hide the content longdesc
          $('#content_longdesc').css('display','none'); 
          $('#nav_more').removeClass('selected');
          $('#nav_more span').html('Read more');
          
        }
    );
  }
  return false;
}
            
               

function show_content(url, screen_height){
  var pos = url.lastIndexOf('.');
  if(pos == -1)return false;
  var ext = url.substring(pos+1);
  var screen = $("#content_screen"); 

  
  if(s_height== undefined) s_height = 283;
  
  if(screen_height!=''){
    s_height = screen_height;
  }

  if($('#content_screen').css('height')!= s_height){
    $('#content_screen').animate({height:s_height},500);
  }
          
  if(!first_run){
      screen.stop(true,true);

      if(ext == 'flv'){
        watch_video(url, screen_height);
        screen.unbind('click',click_show_next);
        screen_click = false;

      }else{
        screen.fadeOut('slow', function(){
                
          if(ext == 'flv'){
            watch_video(url, screen_height);
            screen.unbind('click',click_show_next);
            screen_click = false;
            $(screen).fadeIn();
                    
          }else{
            player = undefined;
            screen.css('display', 'none');
            screen.html('<img src="' + url + '" id="screen_img">');
            screen_img = document.getElementById('screen_img');
            $(screen_img).css('display','none');
          
            screen_img.onload = function(){
              $(this).fadeIn();
              $("#content_screen").fadeIn();
            }
            
            //$("#content_screen").fadeIn();
              
            screen.css('display','block'); 


            if(!screen_click){
              screen_click = true;
              screen.bind('click',click_show_next);
            }
          }                
        });

      }
    

  }  

  if(first_run){
    if(screen){

      if(ext == 'flv'){
        watch_video(url, screen_height);
        screen.unbind('click',click_show_next);
        screen_click = false;         
      }else{
        player = undefined;
        if(!document.all){
          screen.append('<img src="' + url + '" id="screen_img" style="display:none">');
          screen_img = document.getElementById('screen_img');
          screen_img.onload = function(){
            $(this).fadeIn(); 
          }

        }else{
          screen_img = undefined;
          screen_img = document.createElement('img');
          screen.append(screen_img);
          screen_img.onload = function(){
            $(this).fadeIn();
          }
          screen_img.src = url;
          screen_img.id = "screen_img";
          //screen_img.style.display = 'none';

        }
        

        
        if(!screen_click){
          screen_click = true;
          screen.bind('click',click_show_next);
        }
      }
    }
    first_run = false;
  }
  





}
function click_show_next(){
  show_next($(".content_menu .selected"));
  return false;
  
}
function click_show_prev(){
  show_prev($(".content_menu .selected"));
  return false;

}
function show_next(el){
  if($(current_button).parent().next().html()!= null){
    $(current_button).parent().next().children().trigger('click');
  }else{ 
    $(".content_menu>li:first>a").trigger("click");    
  }
}

function show_prev(el){
  if($(current_button).parent().prev().html()!= null){
    $(current_button).parent().prev().children().trigger('click');
  }else{ 
    $(".content_menu>li:first>a").trigger("click");    
  }
}



function watch_video(video_url, height){
  if(s_height == undefined)  s_height = 283;
  if(height!='')s_height = height;

  if(player !=undefined){

    player.sendEvent('LOAD', video_url);
    //alert("VIDEO CHANGED");
    return;
  }
  $('#content_screen').html('Please <a href="" style="color:#F00;font-weight:bold">download flash player</a> to see this video');
  // return;
  var so = new SWFObject( WWW_ROOT + 'flvplayer/player.swf','immix_video','449',s_height,'8');
  so.addParam('allowscriptaccess','sameDomain');
  so.addParam('allowfullscreen','true');
  so.addParam('wmode','transparent');
  
  /*
  so.addVariable('width','449');
  so.addVariable('height','283');
  so.addVariable('quality','high'); 
  so.addVariable('scale','noscale');
  so.addVariable('backColor','#000000');
 
  so.addVariable('file',video_url);
  so.addVariable('autoStart','true');
  */
  
  so.addParam("flashvars","file="+ video_url +"&autostart=true&backcolor=000000&frontcolor=CCCCCC&lightcolor=00DDDD&controlbar=bottom&image=preview.jpg");
  
  so.write('content_screen'); 

}

function playerReady(obj) {
	var id = obj['id'];
	var version = obj['version'];
	var client = obj['client'];
	player = document.getElementById(id);
};

function print_video(video_url, eid){
 // alert(video_url);
  
  var so = new SWFObject( WWW_ROOT + 'flvplayer/player.swf','immix_video','449','283','8');
  so.addParam('allowscriptaccess','sameDomain');
  so.addParam('allowfullscreen','true');
  so.addParam('wmode','transparent');
  so.addParam("flashvars","file="+ video_url +"&autostart=false&backcolor=000000&frontcolor=CCCCCC&lightcolor=00DDDD&controlbar=bottom&image=preview.jpg");
  so.write(eid); 
  
}

/* COOKIE */
function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}