
controller = {};

controller.ORGANIC = 'organic';
controller.ETSY = 'etsy';
controller.ANALYTICS = 'analytics';


controller.info = {	sl:{title:'Etsy Shop Lovers', GA:true, GAstr:'Click to insert your shop/item views data to the calendar below',OAUTH:true, OAUTHstr:'Click to insert your Etsy sales data to the calendar below'},
					tr:{title:'Tag Report', GA:true,GAstr:'Click to insert your Etsy Search data to the Tag Report below',OAUTH:false},
					ee:{title:'Edit Express', GA:false,OAUTH:true, OAUTHstr:'Click to allow batch editing of your Etsy items'},
					faq:{title:'Frequently Asked Questions', GA:false,OAUTH:false,v:'1.0'},
					a:{title:'Advertise on Craftopolis', GA:false,OAUTH:false}
					//tt:{title:'Etsy Treasure Tracks', GA:true,GAstr:'Click to insert treasury click data to Treasure Tracks below',OAUTH:false},
				  };


controller.APIs = [{name:'GA',obj:etsyGoogle},{name:'OAUTH',obj:etsyOauth}];		  
				  
controller.parseParams = function(url,t1,t2){
	var urlParts = url.split(t1);
 
	if (urlParts.length > 1) 
	{
		var rightSide = urlParts[1];
		var outerParts = rightSide.split('&');
 
		for (var outer = 0; outer < outerParts.length; outer++)
		{
			var innerParts = outerParts[outer].split(t2);
			var key = innerParts[0];
			var value = (innerParts.length > 1) ? innerParts[1] : "";
			this.params[key] = value;
		}
	}
	
};

controller.getCookie = function(name){
	name += '=';
	var allCookies = document.cookie;
	var pos = allCookies.indexOf(name);
	
	if (pos != -1){
		var start = pos+name.length;
		var end = allCookies.indexOf(';',start);
		if (end == -1){end = allCookies.length;}
		return allCookies.substring(start,end);
	}
	return '';
};

controller.createCookie = function(name,value,days) {
	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
		
	document.cookie = name+"="+value+expires;
};

controller.deleteCookie = function(name) {
	this.createCookie(name,"",-1);
};

controller.userInputDone = function() {
	this.setHash();

	for (var i in this.APIs) {
		var apiHTML = document.getElementById(this.APIs[i].name);
		if (userInfo.state != userInfo.BEGIN && this.info[this.currentTab.name][this.APIs[i].name]){
			apiHTML.style.visibility = 'visible';
			this.APIs[i].obj.checkStatus();
		}	
		else {
			apiHTML.style.visibility = 'hidden';
		}
	}
	
	document.title = this.info[this.currentTab.name].title;
	this.main.innerHTML = '';
	
	var img = document.createElement('img');
	img.src='images/' + this.currentTab.name + '_subtitle.gif';
	img.className = 'subtitle';
	this.main.appendChild(img);
	if (this.info[this.currentTab.name].v && !Loader.isReady(this.currentTab.name))
		Loader.loadModule(this.currentTab.name,this.info[this.currentTab.name].v,this);
	else this[this.currentTab.name]();
};


controller.loadModule = function(){
	var tab = this.info[this.currentTab.name];
	Loader.addToHeader(this.currentTab.name,tab.v,'css');
	Loader.addToHeader(this.currentTab.name,tab.v,'js');
	Loader.loadHTML(this.currentTab.name,tab.v,this);
}	


controller.readyMod  = function(){
	this[this.currentTab.name]();
}


			  
				  
controller.init = function(){
	google.load('gdata', '2.x', {packages: ['analytics']});
	google.load('visualization', '1', {packages:['table']});
	this.entry = controller.ORGANIC;
	
	this.params = {};
	this.parseParams(window.location.search,'?','=');
	
	
	if (this.getCookie('g314-pending') == ''){
		this.isPending = false;
		var c = this.getCookie('craftopolis');
		this.parseParams(c,'?',':');
	}
	else {
		this.isPending = true;
	}
	
	this.parseParams(window.location.hash,'#','=');
	google.setOnLoadCallback(etsyGoogle.init);
	if (this.params.entry) this.entry = this.params.entry;
	
	
	
};

controller.init();

controller.gaTimeout = function(){
	this.isPending = false;
	this.deleteCookie('g314-pending');
	this.start();
};

controller.start = function(){
	
	if (this.isPending){
		setTimeout('controller.gaTimeout()', 3000);
		return;
	}

	this.deleteCookie('craftopolis');
	
	this.tabs = document.getElementById('tabs');
	this.main = document.getElementById('mainfg');
	
	etsyOauth.init();
	Loader.init();
	
	for (var i = 0; i < this.tabs.childNodes.length; i++){
		this.info[this.tabs.childNodes[i].name].tab = this.tabs.childNodes[i];
		this.tabs.childNodes[i].onclick = this.onclick;
	}
	
	if (this.params.com && this.info[this.params.com]){
		this.setTab(this.info[this.params.com].tab);
	}
	else { 
		this.setTab(this.tabs.firstChild);
	}
	
	userInfo.init();
	
};

controller.setCookie = function(){
	var c = this.getParamString('?',':');
	if (etsyOauth.params.secretToken){
		c += '&secretToken:' + etsyOauth.params.secretToken;}
		
	if (this.entry){
		c+= '&entry:' + this.entry;}
	
	this.createCookie('craftopolis',c);
};

controller.getParamString = function(t1,t2){
	
	var paramStr = t1 + 'com' + t2 + this.currentTab.name;
	if (userInfo.getUserName() != ''){
		paramStr += '&' + 'user' + t2 + userInfo.getUserName();}
		
	return paramStr;
};

controller.setHash = function(){
	window.location.hash = this.getParamString('#','=');
};


controller.setTab = function(tab){
	if (this.currentTab){
		this.currentTab.src = 'images/' + this.currentTab.name + '_up.gif';
	}
	this.currentTab = tab;
	this.currentTab.src = 'images/' + this.currentTab.name + '_down.gif';
	
};

controller.onclick = function(){
	controller.setTab(this);
	controller.userInputDone();
};


controller.tr = function(){
	if (this.genericTest(false,true)){
		TagR.init();
	}
};

controller.tt = function(){
	if (this.genericTest(false,true)){
		TT.init();
	}
};

controller.sl = function(){
	SLQuery.init();
};

controller.ee = function(){

	if (this.genericTest(true,false)){
		EE.init();
	}
};

controller.setHeight = function(height) {
    swfobject.getObjectById("ShopStats").height = height;
	supA.print();
	
};

controller.a = function(){
	supA.print();
};


controller.appendMain = function(str){
	this.main.innerHTML += str;
}

controller.printMsg = function(msg,e){
	var str = '<div class="msg">' + msg + '</div>';
	
	if (e){
		e.innerHTML += str;
	}
	else {
		this.main.innerHTML += str;
	}
};


controller.genericTest = function(needEtsy,needGoogle){
	if (userInfo.state != userInfo.SUCCESS){
		supA.print();
		return false;
	}
	
	if (needGoogle && !etsyGoogle.isLogin()){
		this.printMsg('In order for this feature to work, you\'ll need to sign into your Google Analytics account from above by clicking on the "Import Analytics" button.');
		supA.print();
		return false;
	}
	
	if (needEtsy && !etsyOauth.isLogin()){
		this.printMsg('In order for this feature to work, you\'ll need to sign into your Etsy account from above by clicking on the "Import Etsy" button.');
		supA.print();
		return false;
	}
	
	return true;
};

