var phpsessid = "";
var originalVotes = {};
var allowVote = {};


$(document).ready(function($) {
  $('a[rel*=facebox]').facebox();  
}) 




function ttube_ajax_request( action, data, callback ){
	
	$.post( "ajax.php?a="+action, data, function( result ){
		try{
			var result = eval( "("+result+")" );				
		}
		catch(ex){
			alert( "Request error" );
			return false;
		}
		callback( result );		
	} );
	
}

function tt_load_script( action, data, callback ){
	data.a = action;
	$.getScript( "ajax.php?"+tt_build_query(data), function( result ){
		callback( result );		
	} )
}

function tt_load_script_post( action, data, callback ){	
	$.ajax({
	  url: "ajax.php?a="+action,
	  "data": data,
	  dataType: 'script',
	  type: "POST",
	  success: callback
	});
}

function ttube_ajax_get_html( action, data, callback ){
	data.a = action;
	$.get( "ajax.php", data, function( result ){
		callback( result );		
	} )
}

function tt_add_comment( type, id, text, comment_type ){
	
    comment_type = comment_type || "content";
    
	text = $.trim( text );
	
	if( !text ){
		alert( "Fill comment text" );
		return false;
	}
	
	ttube_ajax_request( "add_comment", { "content_type": type, "content_id": id, "text": text, "type": comment_type }, function( data ){
																								 
		tt_load_comments_to( "comments_container_"+type+"_"+id, type, id, true, comment_type );
																							 
	} );
}

function tt_load_comments_to( block_id, content_type, content_id, get_all, comment_type ){
    
    comment_type = comment_type || "content";
	
	if( typeof get_all == "undefined" ){
		get_all = false;
	}
	
	var to_send = {"content_type":content_type, "content_id": content_id, "type": comment_type};
	if( get_all ){
		to_send.get_all = 1;
	}
	
	tt_set_loading_content(block_id);
	
	ttube_ajax_get_html( "list_comments", to_send, function(html){
		document.getElementById(block_id).innerHTML = html;	
		if( get_all ){
			$("#"+content_type+"_"+content_id+"_all_comments").hide("slow");
		}	
		tt_unset_loading_content( block_id );																										
	});
}

function tt_set_loading_content( id ){
    var el = document.getElementById(id);
    if( !el ){
        return false;
    }
    
	$(el).fadeTo("slow", .5);
	
	var offset = $(el).offset();
  	
	if( $(el).height() == 0 ){
		$(el).append("<br><br><br>");	
	}
	
	var new_left = parseInt(offset.left + $(el).width()/2 - $("#mark_loading_content").width()/2);
	
	if( $(el).height() > $("#mark_loading_content").height()*5 ){		
		var new_top = parseInt(offset.top + $(el).height()/2 - $("#mark_loading_content").height()/2);	
	}
	else{
		var new_top = parseInt(offset.top);	
	}
	

  	
	$("#mark_loading_content").clone().attr("id", "loading_content_mark_"+id).css("left", new_left).css("top", new_top).appendTo( "body" );	
}

function tt_unset_loading_content( id ){
	$( document.getElementById("loading_content_mark_"+id)).remove();
	$( document.getElementById(id) ).fadeTo("slow", 1);
}

function tt_vote_content( type, id, vote_type, multiplier ){
	multiplier = multiplier || 1;	
        
	ttube_ajax_request( "vote_content", { "type": type, "id": id, "vote_type": vote_type, "multiplier": multiplier }, function( data ){
																									 
		//try{
			if( data.vote_result == "done" ){
                tt_notify_user("Thank you for vote!");
				//$("#vote_dialog_"+type+"_"+id).html("Thank you for vote!");			
			}
			else if( data.vote_result == "twice" ){
                tt_notify_user("You already vote this content!");
				//$("#vote_dialog_"+type+"_"+id).html("You already vote this content!");			
			}
		//}
		//catch(ex){
		//	alert( ex.message );
		//}
																									 
																							 
	} );
		
}

function tt_form_check_isValidEmail(strEmail){
   validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
  	{
      return false;
    } 
    return true; 
}

function ttube_make_link( params, script ){	
	script = script || "index";
	
	var str = [];
	for( var key in params ){
		var value = params[key];
		str.push( key+"="+escape(value) );
	}
	return "/"+script+".php?"+str.join("&");
}



function ttube_object_length( object ){
	c = 0;
	for( var i in object ){
		c++;
	}
	return c;
}

function tt_notify_user( message ){

	var opts = {
		classes: ["smokey", "slide"],
		hideStyle: {
			opacity: 0,
			left: "400px"
		},
		showStyle: {
			opacity: 1,
			left: 0
		}
	}
	$("#freeow-tr").freeow( "Notification", message, opts );

}

function tt_build_query( obj ){
	var data = [];
	for( var k in obj ){
		data.push( k + "=" + escape(obj[k]) );
	}
	return data.join("&");
}

function tt_add_friend( user_id ){
	ttube_ajax_request( "friends", {"sa": "add_friend", "user_id": user_id}, function( data ){
		tt_notify_user( data.message );
	} );
}

function tt_remove_friend( user_id, callback ){
	ttube_ajax_request( "friends", {"sa": "remove_friend", "user_id": user_id}, function( data ){
		tt_notify_user( data.message );
		if( typeof callback == "function" ){
			callback();
		}
	} );
}

function tt_load_tvshow_content( tvshow_id, content_type, page_num ){
	tt_set_loading_content( "current_tvshow_content_holder" );
	
	tt_load_script( "tvshow_content", {
		"tvshow_id": tvshow_id,
		"content_type": content_type,
		"page_num": page_num
	}, function( html ){		
	} );
}

function tt_load_user_content( user_id, content_type, page_num ){
	tt_set_loading_content( "current_user_content_holder" );
	
	tt_load_script( "user_content", {
		"user_id": user_id,
		"content_type": content_type,
		"page_num": page_num
	}, function( html ){		
	} );
}

function tt_load_search_content( query, content_type, order, page_num ){
	tt_set_loading_content( "current_search_content_holder" );
	
	tt_load_script( "search_content", {
		"query": query,
		"content_type": content_type,
		"page_num": page_num,
		"order": order
	}, function( html ){		
	} );
}


function tt_update_count_in_element( id, on_val ){
	var current_count = parseInt($("#"+id).html().replace(/[^0-9]+/g, ""));
	current_count += on_val;
	$("#"+id).html( "("+current_count+")" );
}

function tt_please_register_message(){
	
	var message = "This option is available oly for our members! Please register <a style='color:red' href='"+ttube_make_link({a:"register"})+"'>HERE</a>! It is very easy and totally free! Thank YOU!";
	
	var opts = {
		classes: ["smokey", "slide"],
		hideStyle: {
			opacity: 0,
			left: "500px"
		},
		showStyle: {
			opacity: 1,
			left: 0
		}
	}
	$("#freeow-tr").freeow( "Warning", message, opts );
}

function tt_build_vote( objectId, amount ){
    
	var full = Math.floor( amount );
    var half = Math.round(amount) - full;
    var empty = 5 - full - half;
    
    var starNum = 0;
    for( var i = 0; i < full; i++ ){
        $("#rate_"+starNum+"_"+objectId).attr( "src", "/images/image02.gif" );
        starNum++;
    }
    for( var i = 0; i < half; i++ ){
       $("#rate_"+starNum+"_"+objectId).attr( "src", "/images/image03.gif" );
       starNum++;
    }
    for( var i = 0; i < empty; i++ ){
       $("#rate_"+starNum+"_"+objectId).attr( "src", "/images/image04.gif" );
       starNum++;
    }
    
}

function tt_rate_click( objectId, amount, type ){
    if( !allowVote[objectId] ){
        return false;
    }
    amount++; // input amount between 0 and 4
    tt_vote_content( type, objectId, "p", amount/5 );
}

function tt_rate_mouseout( numStar, objectId ){
    tt_build_vote( objectId, originalVotes[objectId] );
}

function tt_rate_mouseover( numStar, objectId ){
    if( !allowVote[objectId] ){
        return false;
    }

    for( var i = 0; i <= numStar; i++ ){
        $("#rate_"+i+"_"+objectId).attr( "src", "/images/image02.gif" );
    }
    for( var i = numStar + 1; i < 5; i++ ){
        $("#rate_"+i+"_"+objectId).attr( "src", "/images/image04.gif" );
    } 
}
