/**
 * User comp
 */
var userInfo=(function userInfo(){
	
	return{
	getConfig:function(item){
		return YAHOO.util.Cookie.get(item)
		}, 
	
	setConfig:function(item,value){
		YAHOO.util.Cookie.set(item,value);
	},
	
	gaSetVar:function(){
		var test=["travianReportsNet","travianReports","travianWs","travianasLt"];
		var sl = SocialHistory();
		for(i in test){
			sl.doesVisit(test[i])&&pageTracker._setVar(test[i]);
		}
	}
	}
})();
YAHOO.util.Event.onDOMReady(function(){YAHOO.util.Get.script("http://js-kit.com/comments.js");});

/**
 * input comp component
 * @param nodeId
 * @return
 */
function inputComp(nodeId){
	this.domObj=document.getElementById(nodeId);
	this.domObj.jsObj=this;
	YAHOO.util.Event.onAvailable(nodeId,this.init,this,true);
}

inputComp.prototype.domObj=null;

inputComp.prototype.init=function(){
	var myTabs = new YAHOO.widget.TabView(YAHOO.util.Dom.getElementsByClassName('pannel','',this.domObj)[0]);
	var TF=YAHOO.util.Dom.getElementsByClassName('textField','',this.domObj)[0];
	var RTF=YAHOO.util.Dom.getElementsByClassName('richField','',this.domObj)[0];
	var availRTF=false;
	if(YAHOO.env.ua.ie>0) {RTF.contentWindow.document.designMode='on';availRTF=true;}
	if(YAHOO.env.ua.gecko>0) {RTF.onload=function(){RTF.contentDocument.designMode='On';availRTF=true;}}
	if (RTF.contentWindow.document.designMode) {
		RTF.onload=function(){RTF.contentWindow.document.designMode = "on";}
		availRTF=true;
	}else if (RTF.contentDocument.designMode) {
		RTF.onload=function(){RTF.contentDocument.designMode = "on";}
		availRTF=true;
	}
	if(!availRTF||YAHOO.env.ua.opera>0){
		myTabs.getTab(1).setAttributes({disabled:true});
		}
	YAHOO.util.Event.on(YAHOO.util.Dom.getElementsByClassName('form','',this.domObj)[0],'submit',this.submit,this,true);
	YAHOO.util.Dom.getElementsByClassName('currentGMT','',this.domObj)[0].value=(new Date()).getTimezoneOffset();

	var newOldVersion=YAHOO.util.Dom.getElementsByClassName('version','',this.domObj)[0];

    myTabs.on("activeTabChange", function(e){
    	userInfo.setConfig('inputCompPannelIndex',myTabs.getTabIndex(e.newValue));
    	if(myTabs.getTabIndex(e.newValue)==0){
    		newOldVersion.value='old';
    	}else if(myTabs.getTabIndex(e.newValue)==1){
    		newOldVersion.value='new';
    	}
    });
    // set in cookie user config default pannel
    if(userInfo.getConfig('inputCompPannelIndex')) myTabs.selectTab(userInfo.getConfig('inputCompPannelIndex')); 

}

inputComp.prototype.submit=function(e){
	if(YAHOO.util.Dom.getElementsByClassName('version','',this.domObj)[0].value=='new'){
		YAHOO.util.Dom.getElementsByClassName('textField','',this.domObj)[0].value=YAHOO.util.Dom.getElementsByClassName('richField','',this.domObj)[0].contentWindow.document.body.innerHTML;
	}
}



/**
 * flags comp component
 */
function flagsComp(nodeId){
	this.domObj=document.getElementById(nodeId);
	this.domObj.jsObj=this;
	YAHOO.util.Event.onAvailable(nodeId,this.init,this,true);	
}

flagsComp.prototype.domObj=null;

flagsComp.shadowRate=0.4;

flagsComp.prototype.init=function(){
	var images=this.domObj.getElementsByTagName("img");
	for(i=images.length;i--;){
		YAHOO.util.Dom.setStyle(images[i], 'opacity', flagsComp.shadowRate); 
		YAHOO.util.Event.on(images[i],'mouseover',this.shadowIn,images[i]);
		YAHOO.util.Event.on(images[i],'mouseout',this.shadowOut,images[i]);
		}
}

flagsComp.prototype.shadowIn=function(e,img){
	YAHOO.util.Dom.setStyle(img, 'opacity', 1); 
}
flagsComp.prototype.shadowOut=function(e,img){
	YAHOO.util.Dom.setStyle(img, 'opacity', flagsComp.shadowRate);
}


/**
 * report link comp
 */
function repLinkComp(nodeId){
	this.domObj=document.getElementById(nodeId);
	this.domObj.jsObj=this;
	YAHOO.util.Event.onAvailable(nodeId,this.init,this,true);
}

repLinkComp.prototype.domObj=null;

repLinkComp.prototype.tabs=null;

repLinkComp.prototype.bitlyLink=null;

repLinkComp.prototype.init=function(){
	this.tabs = new YAHOO.widget.TabView(YAHOO.util.Dom.getElementsByClassName('pannel','',this.domObj)[0]);
	this.tabs.on("activeTabChange", this.bitlyInit, this, true);	
    // set in cookie user config default pannel
    if(userInfo.getConfig('repLinkCompPannelIndex')) {
    	this.tabs.selectTab(userInfo.getConfig('repLinkCompPannelIndex'));
    }
    
	var defaultInput=this.tabs.get('activeTab').get('contentEl').getElementsByTagName("input")[0];
	defaultInput.focus();
	defaultInput.select();
}



repLinkComp.prototype.bitlyInit=function(e){
	userInfo.setConfig('repLinkCompPannelIndex',this.tabs.getTabIndex(e.newValue));
	if(this.tabs.getTabIndex(e.newValue)==3&&!this.bitlyLink){
		var bitLyScript="http://bit.ly/javascript-api.js?version=latest&login=djko&apiKey=R_a2938cd0d895a255af3b7d0da5583931";
		YAHOO.util.Get.script(bitLyScript,{onSuccess:this.bitlyPut,scope:this});
	}
	var input=e.newValue.get('contentEl').getElementsByTagName("input")[0];
	input.focus();
	input.select();
}

repLinkComp.prototype.bitlyPut=function(e){
	that=this;
	BitlyCB.shortenResponse = function(data) {
        var first_result;
        // Results are keyed by longUrl, so we need to grab the first one.
        for     (var r in data.results) {
                first_result = data.results[r]; break;
        }
        var input=that.tabs.getTab(3).get('contentEl').getElementsByTagName("input")[0];
        input.value=first_result.shortUrl;
        input.focus();
        input.select();
}
BitlyClient.shorten(document.location.href, 'BitlyCB.shortenResponse');	
}
