function whenLoadingTopThumbs(){
   // document.getElementById('step_carousel_cover').style.visibility='visible';
   document.getElementById('step_carousel_holder').className='this_loader';
}
function whenCompletedTopThumbs(){
    var thumbsDiv = document.getElementById('step_carousel');
    thumbsDiv.innerHTML = ajax.response;
   // document.getElementById('step_carousel_cover').style.visibility='hidden';
   document.getElementById('step_carousel_holder').className='';   
}
function showTopThumbs(start,modelId){
             curent_start=start;
             if(start%thumbsToShow!=0){
             	previous = parseInt(curent_start - curent_start%thumbsToShow);
             } else{
             	previous = parseInt(curent_start - thumbsToShow);
             }
             if(curent_start==0) disablePrevious();
             if(parseInt(curent_start)+parseInt(thumbsToShow)>= totalModels){disableNext();}else{enableNext();}
                    ajax.requestFile = "index.php";
                    ajax.setVar("_o","Vod");
                    ajax.setVar("_m","getRentalModelsPage");
                    ajax.setVar("start",curent_start);
                    ajax.setVar("model_id",modelId);
                    ajax.method = "GET";
                    ajax.onLoading = whenLoadingTopThumbs;
                    ajax.onCompletion = whenCompletedTopThumbs;
                    ajax.runAJAX();
}
function goToNext(){
     if((curent_start+2*thumbsToShow) <= totalModels){
        next = parseInt(curent_start+thumbsToShow);
     	enableNext();
     }else{
        next = parseInt(totalModels - thumbsToShow);
        disableNext();
     }
     showTopThumbs(next,curent_model_id);
     enablePrevious();
}
function goToPrevious(){
     showTopThumbs(previous,curent_model_id);
}
function disablePrevious(){
      document.getElementById('vr_arrow_left').style.visibility='hidden';
}
function enablePrevious(){
      document.getElementById('vr_arrow_left').style.visibility='visible';
       
}
function disableNext(){
    document.getElementById('vr_arrow_right').style.visibility='hidden';
}
function enableNext(){
     document.getElementById('vr_arrow_right').style.visibility='visible';
}
function whenLoadingModelVideos(){
     var videosDiv = document.getElementById('videos_holder');
     videosDiv.style.display='none'; 
     showBoxPreloader('videos_preloader');
}
function whenCompletedModelVideos(){
    hideBoxPreloader('videos_preloader');
    var videosDiv = document.getElementById('videos_holder');
    videosDiv.style.display='inline' 
    videosDiv.innerHTML = mvajax.response; 
    document.getElementById('myRentedLink').style.display="inline";
    document.getElementById('moreFromThisUserLink').style.display="none";
} 
function showModelVideos(modelId,start){
              if(modelId == 'inherit'){
                       modelId = curent_model_id;
                    }
                    curent_model_id = modelId;
                    currentVideoStart=start;
                    mvajax.requestFile = "index.php";
                    mvajax.setVar("_o","Vod");
                    mvajax.setVar("_m","getModelDataPage");
                    mvajax.setVar("model_id",modelId);
                    mvajax.setVar("start",start);
                    mvajax.method = "GET";
                    mvajax.onLoading = whenLoadingModelVideos;
                    mvajax.onCompletion = whenCompletedModelVideos;
                    mvajax.runAJAX();
}
function whenLoadingVideoComments(){
    var commentsDiv = document.getElementById('comments_holder');
    commentsDiv.style.display='none';
    showBoxPreloader('comments_preloader'); 
}
function whenCompletedVideoComments(){
    hideBoxPreloader('comments_preloader'); 
    var commentsDiv = document.getElementById('comments_holder');   
    commentsDiv.style.display='inline'; 
    commentsDiv.innerHTML = vcajax.response; 
} 
function showVideoComments(videoId,start){
                    if(videoId == 'inherit'){
                       videoId = curent_video_id;
                    } 
                    curent_video_id = videoId;
                    vcajax.requestFile = "index.php";
                    vcajax.setVar("_o","Vod");
                    vcajax.setVar("_m","getModelVideoCommentsPage");
                    vcajax.setVar("ajax","1");
                    vcajax.setVar("video_id",videoId);
                    vcajax.setVar("start",start);
                    vcajax.method = "GET";
                    vcajax.onLoading = whenLoadingVideoComments;
                    vcajax.onCompletion = whenCompletedVideoComments;
                    vcajax.runAJAX(); 
} 
function CheckFieldLength(fn,rn,mc) {
  var len = fn.value.length;
  if (len > mc) {
    fn.value = fn.value.substring(0,mc);
    len = mc;
  }
  document.getElementById(rn).innerHTML = mc - len;
}
function addVideoComment(videoId,userId){
    if(userId == null){
        alert('You must be a registered user in order to add comments.');
    } else {
        var comment = document.getElementById('taMessage').value;
        if(comment!=''){
              addComments_ajax.requestFile = "index.php";
                    addComments_ajax.setVar("_o","Vod");
                    addComments_ajax.setVar("_m","addVideoComment");
                    addComments_ajax.setVar("ajax","1");
                    addComments_ajax.setVar("video_id",videoId);
                    addComments_ajax.setVar("comment",comment);
                    addComments_ajax.method = "GET";
                    addComments_ajax.runAJAX();  
              document.getElementById('taMessage').value = '';
                showVideoComments(videoId,'0');
        }else{
            alert('Please write something');
        }
       }
    return false;    
}
function showBoxPreloader(boxId){
    var box = document.getElementById(boxId).style.display='inline'
}
function hideBoxPreloader(boxId){
    document.getElementById(boxId).style.display='none';
}
function playVideo(modelId,videoId,start){
   thisMovie("player_rentals").pausePlay(modelId,videoId,start);
}
function whenCompletedVideoCheck(){
    var status = checkVideo_ajax.response.split('&');
    for ( i = 0 ; i < status.length ; i += 1 ){
           if (status[i]){
          var key = status[i].split('=');
          if(key[0] && key[1])
           eval(key[0] + "='" + key[1] + "';");
        }
       }
    if(model_start<0){model_start=0;}  
    if(model_exists=='1'){
       if(video_exists=='1'){
              curent_model_id = temp_model_id;
              curent_video_id = temp_video_id;
              curent_start = model_start;
              showTopThumbs(curent_start,curent_model_id);
              addEmbed(curent_model_id,curent_video_id,0);
       		  showModelVideos(curent_model_id,0);
       		  showVideoComments(curent_video_id,'0');

     }else{
        curent_model_id = temp_model_id;
        curent_video_id = first_video;
        curent_start =  model_start;
        showTopThumbs(curent_start,curent_model_id);
        addEmbed(curent_model_id,curent_video_id,0);
        showModelVideos(curent_model_id,0);
        showVideoComments(curent_video_id,'0');
    }
   }else{
       showDefaultModel();
    
   
         
    }
}
function showDefaultModel(){
					curent_start = 0;
                    showTopThumbs(0,curent_model_id);
                    addEmbed(curent_model_id,curent_video_id,0);
                    showModelVideos(curent_model_id,'0');
                    showVideoComments(curent_video_id,'0');
                    var currentVideoStart;

}
function parse_location()
    {
        var s = document.location.hash.substr(1);
        if(s){
        var params = s.split('&');
           for ( i = 0 ; i < params .length ; i += 1 )
            if (params[i]){
                var key = params[i].split('=');
                if(key[0] && key[1]){
                eval("temp_" + key[0] + "='" + key[1] + "';");
                }
                   }
                        checkVideo_ajax.requestFile = "index.php";
                        checkVideo_ajax.setVar("_o","Vod");
                        checkVideo_ajax.setVar("_m","checkModelVideo");
                        checkVideo_ajax.setVar("ajax","1");
                        checkVideo_ajax.setVar("model_id",temp_model_id);
                        checkVideo_ajax.setVar("video_id",temp_video_id);
                        checkVideo_ajax.method = "GET";
                        checkVideo_ajax.runAJAX();
                        checkVideo_ajax.onCompletion = whenCompletedVideoCheck;

                   } else {
                       showDefaultModel();

            }    
    }
/*function addEmbed(modelId,videoId){
var player = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="790" height="468" id="player_rentals" align="middle">';
player +=  '<param name="allowScriptAccess" value="sameDomain" />';
player +=  '<param name="allowFullScreen" value="true" />';
player +=  '<param name="movie" value="flash/player_rentals.swf?movieId=' + videoId + '&modelId=' + modelId + '&startId=0" /><param name="quality" value="high" /><param name="bgcolor" value="#181818" /><embed src="flash/player_rentals.swf?movieId=' + videoId + '&modelId=' + modelId + '&startId=0" allowFullScreen="true" quality="high" bgcolor="#181818" width="790" height="468" name="player_rentals" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
player +=  '</object>';
document.getElementById('vr_player_holder').innerHTML = player; 
}*/
var embed_ajax = new sack();
function addEmbed(modelId,videoId,start){
			embed_ajax.requestFile = "index.php";
			embed_ajax.setVar("_o","Vod");
			embed_ajax.setVar("_m","renderVodSWF");
			embed_ajax.setVar("modelId",modelId);
			embed_ajax.setVar("movieId",videoId);
			embed_ajax.setVar("startId",start);
			embed_ajax.method = "GET";
			/*rentedVideos_ajax.onLoading = whenLoadingModelVideos;*/
			embed_ajax.onCompletion = whenCompletedAddEmbed;
			embed_ajax.runAJAX();
}
function whenCompletedAddEmbed(){
	 eval(embed_ajax.response);
	 //document.getElementById('vr_player_holder_big').innerHTML = embed_ajax.response;
}



var rentedVideos_ajax = new sack();
function whenCompletedRentedVideos(){
    hideBoxPreloader('videos_preloader');
    var videosDiv = document.getElementById('videos_holder');
    videosDiv.style.display='inline'; 
    videosDiv.innerHTML = rentedVideos_ajax.response;
    document.getElementById('myRentedLink').style.display="none";    
    document.getElementById('moreFromThisUserLink').style.display="inline";
}
function showMyRentedVideos(parameter,start){
	   rentedVideos_ajax.requestFile = "index.php";
       rentedVideos_ajax.setVar("_o","Vod");
       rentedVideos_ajax.setVar("_m","memberRentedVideosPage");
       rentedVideos_ajax.setVar("start",start);
       rentedVideos_ajax.method = "GET";
       rentedVideos_ajax.onLoading = whenLoadingModelVideos;
       rentedVideos_ajax.onCompletion = whenCompletedRentedVideos;
       rentedVideos_ajax.runAJAX();
}
function selectHolder(divId){
	var last_thumb;
	for(var i=1;i<=thumbsToShow;i++){
        if(document.getElementById('top_thumb_holder_'+i)){
			document.getElementById('top_thumb_holder_'+i).className='top_thumb';
        }
	}
	document.getElementById(divId).className+=' selected_thumb';
}
