/* 
    WorldCasinoDirectory utility class
    util.js
    All rights reserved.
*/

Ext.BLANK_IMAGE_URL = '/layout/ext/resources/images/aero/s.gif';

// first, declare the namespace if it does not exist
if (WCD == null || typeof(WCD) != "object") { var WCD = new Object();}

WCD.util = {}

WCD.util.loginForm = function () {
		
	var dialog, postLink, viewLink, txtComment;
	var tabs, commentsList, postBtn, renderer;
	var wait, error, errorMsg;
	var posting = false;
	
	return {
		init : function(){
		     // cache some elements for quick access
		     txtComment = Ext.get('comment');
		     commentsList = Ext.get('comments-list');
		     loginLink = Ext.get('wcdUserPanelLoginLink');
		     //registerLink = Ext.get('wcdUserPanelRegisterLink');
		     wait = Ext.get('post-wait');
		     error = Ext.get('post-error');
		     errorMsg = Ext.get('post-error-msg');
		     
		     this.createDialog();
		     
		     loginLink.on('click', function(e){
			 e.stopEvent();
			 tabs.activate('login-tab');
			 dialog.show(loginLink);
		     });
		     
		     /*registerLink.on('click', function(e){
			 e.stopEvent();
			 tabs.activate('register-tab');
			 dialog.show(registerLink);
		     });*/        
		},
		
		// submit the comment to the server
		submitLogin : function(){
		    submitBtn.disable();
		    wait.radioClass('active-msg');
		    
		    var loginSuccess = function(o){
			submitBtn.enable();
			var data = renderer.parse(o.responseText);
			// if we got a comment back
		       /* if(data){
			    wait.removeClass('active-msg');
			    renderer.append(data.comments[0]);
			    dialog.hide();
			}else{
			    error.radioClass('active-msg');
			    errorMsg.update(o.responseText);
			}*/
		    };
		    
		    var loginFailure = function(o){
			submitBtn.enable();
			error.radioClass('active-msg');
			errorMsg.update('Unable to login.');
		    };
		
		    Ext.lib.Ajax.formRequest('login-form', 'logging.php',
			    {success: loginSuccess, failure: loginFailure});          
		},
		
		createDialog : function(){
		    dialog = new Ext.BasicDialog("login-dlg", { 
			    autoTabs:true,
			    width:500,
			    height:300,
			    shadow:true,
			    minWidth:300,
			    minHeight:300
		    });
		    dialog.addKeyListener(27, dialog.hide, dialog);
		    submitBtn = dialog.addButton('Login', this.submitLogin, this);
		    dialog.addButton('Cancel', dialog.hide, dialog);
		
		    
		    // clear any messages and indicators when the dialog is closed
		    dialog.on('hide', function(){
			wait.removeClass('active-msg');
			error.removeClass('active-msg');
			txtComment.dom.value = '';
		    });
		    
		    // stoe a refeence to the tabs
		    tabs = dialog.getTabs();
		    
		    // auto fit the comment box to the dialog size
		    var sizeTextBox = function(){
			txtComment.setSize(dialog.size.width-44, dialog.size.height-264);
		    };
		    sizeTextBox();
		    dialog.on('resize', sizeTextBox);
		    
		    // hide the post button if not on Post tab
		 /*   tabs.on('tabchange', function(panel, tab){
			submitBtn.setVisible(tab.id == 'post-tab');
		    });*/
		    
		}
	};
};
	

//language selection
WCD.util.pickLanguage = function() {
	var languageid = parseInt(document.getElementById("language").value);
	var languageabbrev;
	
	switch(languageid) {
		case 1:
			languageabbrev = 'fr';
			break;
		case 2:
			languageabbrev = 'de';
			break;
		case 5:
			languageabbrev = 'it';
			break;
		case 6:
			languageabbrev = '';
			break;
		case 7:
			languageabbrev = 'es';
			break;
		default:
			break;
	}
	
	var mydate = new Date();
	
	//clear cookie
	mydate.setTime(mydate.getTime() - 100);
	document.cookie = "wcd_language=; expires=" + mydate.toGMTString();

	if(languageid != 6) {
		//set cookie for 10 years
		mydate.setTime(mydate.getTime() + 315360000);
		document.cookie = "wcd_language="+languageabbrev+"; expires=" + mydate.toGMTString() + "; path=/";
	}

	if(undefined!==availableTranslations[languageid]){
		document.location.href = availableTranslations[languageid];
	} else {
		GB_showCenter('Language preference set', 'http://www.worldcasinodirectory.com/'+languageabbrev+'/languageset');
	}
};


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain=.worldcasinodirectory.com";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}