/**
 * @author aberbery
 */
var TAG_TYPE_KEYWORDS = 0;		
var TAG_TYPE_ENGINES = 1;
var TAG_TYPE_SITES = 2;
var TAG_TYPE_ENGINES_KEYWORDS = 10;
var TAG_TYPE_ENGINES_SITES = 12;

var GROUP_BY_KEYWORDS = "keyword";
var GROUP_BY_ENGINES = "engine";
var GROUP_BY_SITES = "site";	

var SEO = new Object();

var moreEnginesDialog;

SEO.Calendar = function(_container, _obj, _month){	
	var container = _container;
	var month = _month
	if(!Utils.exist(month)){ 
		month = new Date().getMonth();
	}
	var lang = Framework.Lang;
	var obj = _obj;
	var months = 
	[
		lang.Calendar_Jan, lang.Calendar_Feb, lang.Calendar_March, lang.Calendar_April,
		lang.Calendar_May, lang.Calendar_June, lang.Calendar_Jul, lang.Calendar_Aug,
		lang.Calendar_Sep, lang.Calendar_Oct, lang.Calendar_Nov, lang.Calendar_Dec, "Last 7 Days"
	]
	
	this.getCurrentMonth = function(){
		return new Date().getMonth();
	}
	
	this.getMonth = function(){
		return month;
	}
	
	this.setMonth = function(_month){
		month = _month;
	}
	
	this.changeToNextMonth = function(){
		if(month == 11){
			month = 0;
		} else {
			month++;
		}
		return month;
	}
	
	this.changeToPreviousMonth = function(){
		if(month == 0){
			month = 11;
		} else {
			month--;
		}
		return month;
	}
	
	this.getMonthName = function(monthNumber){
		return months[monthNumber];
	}	
	
	this.getContainer = function(){
		return container;
	}
	
	this.setContainer = function(_container){
		container = _container;
	}
	
	this.getObj = function(){
		return obj;
	}
	
	this.setObj = function(_obj){
		obj = _obj
	}
}
/**
 * @author Nare Gasparyan
 */
var DropDownMenu = function(){
	
	var self = this;
	this.removeMenu = function() { 
		var menu = document.getElementById("topMenu");
		if(menu && menu.parentNode){		
			menu.parentNode.removeChild(menu);
		}
	}

	this.showList = function(list, ev, parentHeight, activeIndex, verticalPlace, horizontalPlace, parentWidth){
		var topTenDiv = document.getElementById("topMenu");
		var target = ev.target || ev.srcElement;
		if (Utils.exist(topTenDiv)) {
			document.body.removeChild(topTenDiv);
			document.onclick=null;
		} 
		var xAdjust = 3;
		var yAdjust = 5;
		var Browser = checkBrowserVersion();
		if(Browser.isIE){
			yAdjust = 8;	
		}
		if(!Utils.exist(topTenDiv) || topTenDiv.getAttribute("sourceId") != target.id){

			
			var div = document.createElement("div");
			div.id  = "topMenu";
			div.setAttribute("sourceId", target.id);
			var position = Utils.getElementPosition(target);			
			var x = position.x;
			var y = position.y;
  			//var x = ev.clientX;
  			//var y = ev.clientY;

			var clickFunction = function(event, params){
				var listItem = params.listItem;				
				listItem.active = "true";
				listItem.click(this);
				dropDownMenu.removeMenu();
			} 
			for(var i = 0; i < list.length; i++){
				var subDiv = document.createElement("div");
				if(i == activeIndex){
					subDiv.setAttribute("active", "true");
					subDiv.className = "top-menu-item-active";
				} else {
					subDiv.setAttribute("active", "false");
					subDiv.className = "top-menu-item";
				}				
				subDiv.id = "menu_"+list[i].id;
				if(Utils.exist(list[i].group)){
					subDiv.setAttribute("group", list[i].group);
				}
				if(Utils.exist(list[i].show)){
					subDiv.setAttribute("show", list[i].show);
				}
				if(Utils.exist(list[i].number)){
					subDiv.setAttribute("number", list[i].number);
				}
				if(Utils.exist(list[i].realId)){
					subDiv.setAttribute("realId", list[i].realId);
				}				
				subDiv.setAttribute("arrayIndex", i);
				subDiv.innerHTML += list[i].title;				
				subDiv.onmouseover = function(){
					this.className = "moreItemOver";
				}				
				subDiv.onmouseout = function(){
					if(this.getAttribute("active") == "true"){
						this.className ="top-menu-item-active";
					} else {
						this.className ="top-menu-item";
					}
					
				}

				YAHOO.util.Event.addListener(subDiv, "click", clickFunction, {listItem: list[i], index:i});				
				div.appendChild(subDiv);
			}
			switch(verticalPlace){
				case "top":{
					var dropDownSizes = Utils.getHtmlSize(div.innerHTML);
					div.style.left = (x + xAdjust) + "px";
					div.style.top = (y - dropDownSizes.y - yAdjust) + "px";
					break;
				}
				case "bottom":{
					div.style.left = (x + xAdjust) + "px";
					div.style.top = (y + parentHeight + yAdjust) + "px";
					break;
				}				
				default:{
					var dropDownSizes = Utils.getHtmlSize(div.innerHTML);
					div.style.left = (x + xAdjust) + "px";
					div.style.top = (y - dropDownSizes.y - yAdjust) + "px";
					break;
				}
			}
			switch(horizontalPlace){
				case "left":{
					div.style.left = (x + xAdjust) + "px";
					break;
				}
				case "right":{
					var dropDownSizes = Utils.getHtmlSize(div.innerHTML);
					div.style.left = (x - xAdjust + parentWidth - dropDownSizes.x) + "px";
					break;
				}				
				default:{
					div.style.left = (x + xAdjust) + "px";
					break;
				}				
			}

			document.body.appendChild(div);
			document.onclick = function(ev){
				ev = (ev) ? ev : window.event;
				target = ev.srcElement || ev.target;
				if ( target.id.indexOf("Menu") < 0){
					dropDownMenu.removeMenu();
				}
			}
		}
	}
}
var dropDownMenu = new DropDownMenu();

SEO.Calendar.Renderer = {
	
	getCalendarTable: function(seoCalendar){
		var currentMonth = seoCalendar.getMonth();	
		var obj = seoCalendar.getObj();	
		var table = document.createElement("table");
		table.width = "100px";
		var tr = document.createElement("tr");
		table.appendChild(tr);
		var td = document.createElement("td");
		tr.appendChild(td);
		td.onclick = obj.changeToPreviousMonth;
		td.width = "15px";
		td.style.cursor = "pointer";
		var prevImgNode = new Image();
		prevImgNode.src = "images/prevDay.gif";
		td.appendChild(prevImgNode);
		
		var tdCenter = document.createElement("td");
		tr.appendChild(tdCenter);
		tdCenter.width = "70px";
		tdCenter.align = "center";		
		var textNode = document.createTextNode(seoCalendar.getMonthName(currentMonth));
		tdCenter.appendChild(textNode);
		
		td = document.createElement("td");
		tr.appendChild(td);
		td.onclick = obj.changeToNextMonth;
		td.width = "15px";
		td.style.cursor = "pointer";
		var nextImgNode = new Image();
		nextImgNode.src = "images/nextDay.gif";
		td.appendChild(nextImgNode);
				
		return table;	
	}
	
	
}



var MoreEnginesDialogWindow = function(){			
	var reCall = true;
	var dialog;	
	this.move = function(dialog, x, y){			
		dialog.show();
	}
	this.show = function(){			
		dialog.show();
	}
	this.fixDialogSizes = function() {
		var dialogWindowBodyId = dialog.body.id;
		var dialogWindowBody = dialog.body;
		if(Utils.exist(dialogWindowBodyId)){
			dialogWindowBody = $(dialogWindowBodyId);
			var anasunNode = $("dummyNode");	
			if(reCall){
				anasunNode.innerHTML = dialogWindowBody.innerHTML;
				setTimeout( "moreEnginesDialog.fixDialogSizes()", 200 );
				reCall = false;
				return;
			}
			reCall = true;
			var width  = anasunNode.scrollWidth + 100;
			var height = anasunNode.scrollHeight + 80;
			dialog.resizeTo(width, height);
			anasunNode.innerHTML = "";
		}
	}	
	this.init = function(){
		dialog = new Ext.BasicDialog("seo_add-dlg", {
								modal:true,
								width:520,
								height:400,
								shadow:true,
								proxyDrag: true,
								resizable: false,
								autoScroll: true,
								draggable: true
            			});
		dialog.addListener("move", this.move);			
	}
	this.init();
}

Framework.Modules.SEOTree = function(){
	
	var self = this;
	this.createSEOModuleMenu = function(){
		
		var menuNew = new Ext.menu.Menu({
			id: 'menuNew'
		});
		tb.add({
				cls: 'x-btn-text-icon bmenu',
				text:'New',
				menu: menuNew
		});		
			
	  	var addSEOBody = $("addSEOBody");

		var items = {};
		
		items["AddSEO"] = 					{ mode: "SEO", 					width: 315, height: 440, className:"iconStyle iconAddSEOPositionBig", img:"images/monitis/16x16/position_add.gif"};
		items["AddSEOBacklinks"] = 			{ mode: "SEOBacklinks", 		width: 315, height: 380, className:"iconStyle iconAddSEOBacklinksBig", img:"images/monitis/16x16/backlinks_add.gif"};
		items["AddSEOIndexedPages"] = 		{ mode: "SEOIndexedPages", 		width: 315, height: 375, className:"iconStyle iconAddSEOIndexedPagesBig", img:"images/monitis/16x16/indexed_pages_add.gif"};
		items["AddSEOPageRank"] = 			{ mode: "SEOPageRank", 			width: 315, height: 350, className:"iconStyle iconAddSEOPageRankBig", img:"images/monitis/16x16/page_ranks_add.gif"};
		items["AddSEORanking"] = 			{ mode: "SEORanking", 			width: 315, height: 390, className:"iconStyle iconAddSEORankingBig", img:"images/monitis/16x16/ranking_add.gif"};
		items["AddSEOSocialBookmarks"] =	{ mode: "SEOSocialBookmarks", 	width: 315, height: 500, className:"iconStyle iconAddSEOSocialBookmarksBig", img:"images/monitis/16x16/social_bookmarks_add.gif"};
		items["AddSEOMyBuzz"] =				{ mode: "SEOMyBuzz", 			width: 315, height: 340, className:"iconStyle iconAddSEOSocialBookmarksBig", img:"images/monitis/16x16/mybuzz_add.gif", menuOnly:true};
		items["AddContact"] = 				{ mode: "Contact", 		    	width: 305, height: 240, className:"iconStyle iconNotification", img:"images/monitis/16x16/add_contact.gif", menuOnly:true};


		addSEOBody.innerHTML = "";

		for(var addname in items) {
			if( typeof(Framework.Lang.Menu[ addname ]) == "function" )continue;
			if(items[addname].menuOnly == true){
				self.generateAddDialog(menuNew, addname, items[addname]["mode"], items[addname]["img"], items[addname]["width"], items[addname]["height"]);
			} else {
				var menuItem = "";
				menuItem += "<li class='menuItem' >";
				menuItem += 	'<div class="container" id="'+ addname +'" dialogWidth="'+items[addname]["width"]+'" dialogHeight="'+items[addname]["height"]+'"    onclick="Controller.showDialogPanel( event,\''+items[addname]["mode"]+'\')" align="center">';			
				menuItem += 		"<div class='"+ items[addname]["className"] +"' align='center' dialogWidth='"+items[addname]["width"]+"' dialogHeight='"+items[addname]["height"]+"'></div>";
				menuItem += 		"<span class='itemName' dialogWidth='"+items[addname]["width"]+"' dialogHeight='"+items[addname]["height"]+"'>"+Framework.Lang.Menu[ addname ]+"</span>";
				menuItem += 	"</div>";
				menuItem += "</li>";
				self.generateAddDialog(menuNew, addname, items[addname]["mode"], items[addname]["img"], items[addname]["width"], items[addname]["height"]);
				addSEOBody.innerHTML += menuItem ;
			}
		}
		
	}
	this.generateAddDialog = function(menuToAdd, itemParam, modName, icon, width, height){		
		var actionFunc = function(event){
			Controller.showDialogPanel( event, modName, null, width, height);
		};
		var item = menuToAdd.add({
			text: Framework.Lang.Menu[itemParam],
			handler: actionFunc,
			icon: icon
		});
	}
}