//## ! SITE CONFIGURATION !
// DB initialization 
var dbArray = new Array();
var dbreport;
var recordcount;
var coloumcount;

// Ajax initialization 
var cp = new cpaint();
cp.set_debug(false);			
cp.set_response_type('XML');


//!!!!!!!!! Executer Database
var dbLloaded=true;

function executeDB(sql){
	var processScriptUrl=baseurl+'ajax/myphpajax.php';
	cp.call(processScriptUrl,'executeDB',return_value,sql);
}
	
function return_value(result){
				
	dbreport=result.getElementsByTagName('dbreport').item(0).firstChild.data;
	
	if (dbreport=='1'){
		// GET How many row fetch
		recordcount=result.getElementsByTagName('recordcount').item(0).firstChild.data;
		coloumcount=result.getElementsByTagName('coloumcount').item(0).firstChild.data;

		//Population My DB Values							
		for( i=0;i<recordcount;i++){
			dbArray[i]=new Array(recordcount);
			for( ii=0;ii<coloumcount;ii++){
				dbArray[i][ii]=result.getElementsByTagName('dbvalue'+i+ii).item(0).firstChild.data;															
			}
		}	
	}
	dbLoaded = false;															
}
//END 

//!!!!!!!Insert data in to table HTML page
function insertInToTable(tblId, varRow, varCell, varData){
	try{
		var x=document.getElementById(tblId).rows[varRow].cells;
		x[varCell].innerHTML=varData;
		return false;
	}
	catch(Err){
		return false;
	}
}

// !!!!!! Input validation empty
function txtBoxValidation(myId,defaultColor,errColor){
	// # My property
	try{	
		me=document.getElementById(myId);

		if(me.value==""){	
			me.style.background=errColor;
			me.setFocus;
			return false;
		}
		else{
			me.style.background=defaultColor;
			me.setFocus;
			return true;
		}
	}
	catch(Err){
		return 'Err';
	}
}
// END 
   
// !!!!!! HIDE OBJECT
function hideMe(myId){
	document.getElementById(myId).style.display="none";
}

// !!!!!! SHOW OBJECT 
function showMe(myId){
	document.getElementById(myId).style.display="block";
}

// !!!!!!  ASSIGN VALUE IN TO OBJECT
function directMyvalueto(myValue,thatId){
	document.getElementById(thatId).value=myValue;
}
// END 

var timeOutID;
var menuStyle;
var menuHideID;

function menuDropDown(menuID, mouseover) 
{
	if (menuID != menuHideID){
		if (menuHideID) {
			menuStyle.display	= 'none';
		}
		menuHideID	= menuID;
	}	
	
	var menuElem	= document.getElementById(menuID);
	menuStyle		= menuElem.style;
	
	if (mouseover){
		menuStyle.display 	= 'block';
		clearTimeout(timeOutID);
	} else {
		timeOutID 	= setTimeout("menuStyle.display	= 'none';", 1500);
	}
}

function statusText(id, text, hide) 
{
	var element = document.getElementById(id);
	
	// focus on input > delete text
	if (hide && element.value == text) {
		element.value = '';
		document.getElementById('status_input').style.borderColor = '#C95C0C';
		document.getElementById('status_input').style.color = '#ccc';
	}
	// blur on input > insert text
	else if (!hide && element.value == '') {
		element.value = text;
		document.getElementById('status_input').style.borderColor = '#666666';
		document.getElementById('status_input').style.color = '#8e9092';
	}
	
	return false;
}

function toggleAll(element) 
{
	var form = document.forms.openinviter, z = 0;
	for(z=0; z<form.length;z++)
		{
		if(form[z].type == 'checkbox')
			form[z].checked = element.checked;
	   	}
}

function fxRate(uid,rate,idToHide,idToShow,id,table,field)
{
	if ( uid == '0' ) {
		window.location = "../../signup.php"
	} else {
		cp.call(baseurl+'ajax/myajaxphp.php','process_data',return_data,rate,id,table,field);
		hideMe(idToHide);
		showMe(idToShow);
	}
}

function return_data(restul) 
{			
	// Collect the number of BLUE star
	var cnt=restul.getElementsByTagName('trate').item(0).firstChild.data;				
			hideMe('idViewVoteResult');
	if(cnt!='exist')
	{
		// # Show the vote 
		var x=document.getElementById('idVoteView').rows[0].cells;
			x[1].innerHTML=restul.getElementsByTagName('tvote').item(0).firstChild.data;
		
			if(cnt>6)
			{
				cnt=6;
			}	
			else if (cnt<0)
			{
				cnt=0;
			}
				
			blank_star=6-cnt;

			var x=document.getElementById('tblViewResult').rows[0].cells;
			
			for (i=0;i<cnt;i++ )
			{
				x[i].innerHTML='<img src='+imgurl+'/star.gif>';
			}
				
			for (j=cnt;j<5;j++ )
			{
				x[j].innerHTML='<img src=' + imgurl+'/blank_star.gif>';
			}
	}
	else
	{
		var x=document.getElementById('voteProcessthank').innerHTML="<FONT COLOR=#FF0000 >You already vote this video</FONT>";
	}
		
	return false;
} 

function fxSendComments(idToHide,commentId,uid,id,where){
	comment_value=document.getElementById(commentId).value;
	if(comment_value==''){
		alert(' Comment box is empty !!');
	}
	else{
		hideMe(idToHide);	
		cp.call(baseurl+'/ajax/myajaxphp.php','process_comments',return_comment_response,comment_value,uid,id,where);
	}
}

function return_comment_response(restul) {
	msg_number = restul.getElementsByTagName('a').item(0).firstChild.data;
	if ( msg_number==0 ){
		showMe('divComResult2');
	} else if ( msg_number==1 ){
		showMe('divComResult1');
	}
}

function fxSendGifts(idToHide,uid,id){
	giftId=document.getElementById('giftId').value;
	giftTxt=document.getElementById('giftTxt').value;
	if (document.getElementById('giftPrv').checked == 1) giftPrv=1; else giftPrv=0;
	giftCode=document.getElementById('giftCode').value;

	if(giftId==''){
		alert('Παρακαλώ επιλέξτε ένα δώρο!');
	}
	else if(giftTxt=='') {
		alert('Παρακαλώ γράψτε μία αφιέρωση!');
	}
	else{
		hideMe(idToHide);	
		cp.call(baseurl+'/ajax/myajaxphp.php','process_gifts',return_gift_response,giftId,giftTxt,giftPrv,giftCode,uid,id);
	}
}

function return_gift_response(restul) {
	msg_number = restul.getElementsByTagName('a').item(0).firstChild.data;
	if ( msg_number==0 ){
		showMe('divGiftResult2');
	} else if ( msg_number==1 ){
		showMe('divGiftResult1');
	}
}

function fxReportComment(idToHide,comid) {
	hideMe(idToHide);	
	cp.call(baseurl+'/ajax/myajaxphp.php','process_reports',return_comment_report,comid);
}

function return_comment_report(restul) {
	msg_number = restul.getElementsByTagName('a').item(0).firstChild.data;
	com_id = restul.getElementsByTagName('b').item(0).firstChild.data;
	if ( msg_number==0 ){
		showMe('divComReport2_'+com_id);
	} else if ( msg_number==1 ){
		showMe('divComReport1_'+com_id);
	}
}

function fxFeature( uid, id, field, table ) {
	if ( uid == '0' ) {
		hideMe('featureLink');
		hideMe('featureSuccess');
		hideMe('featureFailed');
		showMe('featureLogin');
	} else {
		cp.call(baseurl+'/ajax/myajaxphp.php','featureItem', featureResponse, uid, id, field, table);
	}
}

function featureResponse( feature_result ) {
	var feature_response_id=feature_result.getElementsByTagName('featureMessage').item(0).firstChild.data;
	if ( feature_response_id == '0' ) {
		hideMe('featureLink');
		hideMe('featureFailed');
		hideMe('featureLogin');
		showMe('featureSuccess');
	} else {
		hideMe('featureLink');
		hideMe('featureSuccess');
		hideMe('featureLogin');
		showMe('featureFailed');
	}
}

function checkEnter(e, uid, status){ 
	var characterCode;

	if(e && e.which){ 
		e = e
		characterCode = e.which 
	}
	else{
		e = event
		characterCode = e.keyCode 
	}

	if(characterCode == 13){ 
		fxEditStatus( uid, status);
		return false
	}
	else{
		return true
	}
}

function fxEditStatus( uid, status) {
	if ( status.length <= '3' ) {
		hideMe('editStatusSuccess');
		hideMe('editStatusFailed');
		showMe('editStatusSort');
	}
	else if ( uid == '0' ) {
		hideMe('editStatusSuccess');
		showMe('editStatusFailed');
		hideMe('editStatusSort');
	} else {
		cp.call(baseurl+'/ajax/myajaxphp.php','edit_status', editStatusResponse, uid, status);
	}
}

function editStatusResponse( fav_result ) {
	var fav_response_id=fav_result.getElementsByTagName('addStatusMessage').item(0).firstChild.data;
	if( fav_response_id == '0' ) {
		hideMe('editStatusFailed');
		showMe('editStatusSuccess');
	} else {
		hideMe('editStatusSuccess');
		showMe('editStatusFailed');
	}
}

function fxAddFavorite( hidediv, uid, id, itemuid, field, table) {	
	hideMe(hidediv);
	if ( uid == '0' ) {
		hideMe('addToFavLink');
		hideMe('addToFavSuccess');
		hideMe('addToFavFailed');
		hideMe('addToFavAlready');
		hideMe('addToFavOwner');
		showMe('addToFavLogin');	
	} else if ( uid == itemuid ) {	
		hideMe('addToFavLink');
		hideMe('addToFavSuccess');
		hideMe('addToFavFailed');
		hideMe('addToFavAlready');
		hideMe('addToFavLogin');
		showMe('addToFavOwner');
	} else {
		cp.call(baseurl+'/ajax/myajaxphp.php','addToFavorites', addToFavoritesResponse, uid, id, field, table);
	}
}

function addToFavoritesResponse( fav_result ) {
	var fav_response_id=fav_result.getElementsByTagName('addFavMessage').item(0).firstChild.data;
	if( fav_response_id == '2' ) {
		hideMe('addToFavLink');
		hideMe('addToFavSuccess');
		hideMe('addToFavFailed');
		hideMe('addToFavLogin');
		hideMe('addToFavOwner');
		showMe('addToFavAlready');
	} else if( fav_response_id == '0' ) {
		hideMe('addToFavLink');
		hideMe('addToFavFailed');
		hideMe('addToFavAlready');
		hideMe('addToFavLogin');
		hideMe('addToFavOwner');
		showMe('addToFavSuccess');
	} else {
		hideMe('addToFavLink');
		hideMe('addToFavSuccess');
		hideMe('addToFavAlready');
		hideMe('addToFavLogin');
		hideMe('addToFavOwner');
		showMe('addToFavFailed');
	}
}

function fxChat( hidediv, uid, target_uid) {	
	hideMe(hidediv);
	cp.call(baseurl+'/ajax/myajaxphp.php','chatNow', chatResponse, uid, target_uid);
}

function chatResponse( chat_result ) {
	var chat_response_id = chat_result.getElementsByTagName('chatMessage').item(0).firstChild.data;
	if( chat_response_id == '2' ) {
		hideMe('addToChatSuccess');
		hideMe('addToChatFailed');
		showMe('addToChatGold');
	} else if( chat_response_id == '0' ) {
		hideMe('addToChatFailed');
		hideMe('addToChatGold');
		showMe('addToChatSuccess');
	} else {
		hideMe('addToChatSuccess');
		hideMe('addToChatGold');
		showMe('addToChatFailed');
	}
}

function showRelatedVideos()
{
    var tabRelatedVideos = document.getElementById('tabRelatedVideos');
    var tabUservideos = document.getElementById('tabUserVideos');
    tabUservideos.className = "";
    tabRelatedVideos.className = "tabactive";
    hideMe('userVideos');
    showMe('relatedVideos');
}

function showUserVideos()
{
    var tabRelatedVideos = document.getElementById('tabRelatedVideos');
    var tabUservideos = document.getElementById('tabUserVideos');
    tabRelatedVideos.className = "";
    tabUservideos.className = "tabactive";
    hideMe('relatedVideos');
    showMe('userVideos');
}

//rotating thumb functions
var changing_thumbs = new Array();
function changeThumb(index, i, num_thumbs, path)
{
    if (changing_thumbs[index])
    {
        if (i == '6') {   	
        	document.getElementById(index).src = path + "/" + i + "_" + index + ".jpg";
        	i = '1';
        }	
        else {
        	document.getElementById(index).src = path + "/" + i + "_" + index + ".jpg";
        	i++;
        }
  
        setTimeout("changeThumb('" + index + "'," + i + ", " + num_thumbs + ", '" + path + "')", 600);
    }
}
    
function startThumbChange(index, num_thumbs, path)
{    
    changing_thumbs[index] = true; 
    changeThumb(index, 1, num_thumbs, path);
}

function endThumbChange(index, path)
{
    changing_thumbs[index] = false;
    document.getElementById(index).src = path;
}

function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function toggleTab( tab_id, button_id ) {
	for(i=0; i<buttons.length; i++ ) {
		elem = document.getElementById(buttons[i]);
		if ( buttons[i] == button_id ) {
			elem.className="tabactive";
		} else {
			elem.className="";
		}
	}

	for(i=0; i<tabs.length; i++ ) {
		elem = document.getElementById(tabs[i]);
		if(tabs[i]==tab_id) {
			elem.style.display="block";
		} else {
			elem.style.display="none";
		}
	}
}