//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupWidgetStatus = 0;

var tagbug_width = 270;
var tagbug_height = 250;
var tagbug_cornerRadius = 0;
var tagbug_outerGlow = 10;
var tagbug_borderWidth = 3; 
var tagbug_borderColor = "#378bcb";
var tagbug_bgTopColor =  "#2c6ea2";
var tagbug_bgButColor = "#0f2636";
var tagbug_buttonColor = "#000000";
var tagbug_textName = "Helvetica";
var tagbug_textColor = "#d2ebff";

function clearDefault(el) {
	  if (el.defaultValue==el.value) el.value = "";
}

function tagbug_addPopupWidget(response){
	var content = tagbug_createWidgetList(response);
	//var actions = tagbug_createWidgetAction(response);
	var html = 	'<div id="tagbug_newWidget"><div id="tagbug_popupWidget">' +
				'<div id="tagbug_popupWidgetClose"></div>' +
				'<div id="tagbug_wigetHead">' +
				'<div id="tagbug_widgetLogo"></div>' +
				'<div id="tagbug_widgetSearch"><input id="tagbug_widgetSearchText" type="text" onFocus="clearDefault(this);" ' +
				'value="Search for web site" onkeyup="tagbug_searchBookmark();"/></div>' +
				'</div>' +
				'<div id="tagbug_widgetContent">' +
				'' + content +
				'</div>' +
				'</div>' +
				'<div id="tagbug_backgroundPopupWidget"></div></div>';
	jQuery(document.body).append( html );
}

function tagbug_createWidgetList(response){
	var content = '<ul id="tagbug_ul1">';
	var settings = eval( "(" + response + ")" );
	var icons = settings.ico;
	if(icons != null && icons.length != 0)
            {
                for(var i = 0; i< icons.length; i++)
                {
			content	+= '<li id="tagbug_li1_'+icons[i].id+'" class="tagbug_list1">' +
				   '<a href="'+path+'save_service.php?url='+encodeURIComponent(window.location)+'&w_id='+widgetId+
				   '&id='+icons[i].id+'&type='+encodeURIComponent( icons[i].type)+
				   '&title='+encodeURIComponent(document.title)+
				   '" target="_blank" onclick="tagbug_disablePopupWidget();">' +
				   '<img src="'+path+icons[i].path+'" class="tagbug_list_img" title="'+ icons[i].title +'"/>' +
				   '' + icons[i].title +
				   '</a>' +
				   '</li>';
                }
            }
	content += '</ul>';
	return content;
}

function tagbug_createWidgetAction(response){
	var content = '<div><script type="text/javascript">jQuery(document).ready(function(){';
	var settings = eval( "(" + response + ")" );
	var icons = settings.ico;
	if(icons != null && icons.length != 0)
            {
                for(var i = 0; i< icons.length; i++)
                {
                	content	+= 'jQuery("#li1_'+icons[i].id+'").bind("click",function(e){ tagbug_submitService("'+icons[i].id+'","'+icons[i].type+'");});';
                }
            }
	content += '});</script></div>';
	return content;
}

function tagbug_submitService(id, type){
	tagbug_disablePopupWidget();

    window.open(path+"save_service.php?url="+encodeURIComponent(window.location)+"&w_id="+widgetId+"&id="+id+"&type="+encodeURIComponent(type)+"&title="+encodeURIComponent(document.title), "Tagbug redirect","");
    //alert(path+"save_service.php?url="+encodeURIComponent(window.location)+"&w_id="+widgetId+"&id="+id+"&title="+encodeURIComponent(document.title)+"&type="+encodeURIComponent(type));
};


function tagbug_searchBookmark(){
	var searchKey = jQuery("#tagbug_widgetSearchText").val();
	var loadUrl = path + "/searchKey.php?w_id="+widgetId+"&key="+searchKey+"&callback=?";
	jQuery.getJSON(loadUrl,
		function(response){
			var content = tagbug_createWidgetList(response.settings);
			jQuery("#tagbug_widgetContent").html(content);
			jQuery('#tagbug_popupWidget #tagbug_ul1 a').css("color",tagbug_textColor);
			jQuery('#tagbug_popupWidget #tagbug_ul1 a').css("font-family",tagbug_textName);
		}
	);
}

//loading popup with jQuery magic!
function tagbug_loadPopupWidget(){
	//loads popup only if it is disabled
	if(popupWidgetStatus==0){
		jQuery("#tagbug_backgroundPopupWidget").css({
			"opacity": "0.7"
		});
		jQuery("#tagbug_backgroundPopupWidget").fadeIn("fast");
		jQuery("#tagbug_popupWidget").fadeIn("fast");
		
		
		
		popupWidgetStatus = 1;
	}
}

//disabling popup with jQuery magic!
function tagbug_disablePopupWidget(){
	//disables popup only if it is enabled
	if(popupWidgetStatus==1){
		jQuery("#tagbug_backgroundPopupWidget").fadeOut("fast");
		jQuery("#tagbug_popupWidget").fadeOut("fast");
		popupWidgetStatus = 0;
	}
}

//centering popup
function tagbug_centerPopupWidget(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#tagbug_popupWidget").height();
	var popupWidth = jQuery("#tagbug_popupWidget").width();
	//centering
	jQuery("#tagbug_popupWidget").css({
		"position": "fix",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	jQuery("#tagbug_backgroundPopupWidget").css({
		"height": windowHeight
	});
}


function tagbug_LoadCSS(url){
 	var oLink = document.createElement("link");
 	oLink.href = url;
 	oLink.rel = "stylesheet";
 	oLink.type = "text/css";
 	document.getElementsByTagName("head")[0].appendChild(oLink);
}

function tagbug_LoadScript(url){
 	var e = document.createElement("script");
 	e.src = url;
 	e.type="text/javascript";
 	document.getElementsByTagName("head")[0].appendChild(e); 
}

if (typeof jQuery =='undefined'){ 		
	tagbug_LoadScript(path+'/js/jquery-1.4.2.min.js'); 		
}

//CONTROLLING EVENTS IN jQuery
jQuery(function(){
	//url path to create widget button
	var loadUrl = path + "/load_button.php?w_id=" + widgetId +"&callback=?";
	
	//load script and css on load 
	if (typeof jQuery.dimensions == 'undefined') tagbug_LoadScript(path+'/js/jquery.dimensions.js');
	if (typeof jQuery.fn.gradient == 'undefined') tagbug_LoadScript(path+'/js/jquery.gradient.js');
	tagbug_LoadCSS(path+'/css/v2.css');
	
	jQuery.getJSON(loadUrl,
			function(response){
				jQuery("#TBwidget").html(response.id);

				tagbug_addPopupWidget(response.settings);
				
				var settings = eval( "(" + response.settings + ")" );
				
				//tagbug_width 		= settings.width;
				//tagbug_height 	= settings.height;
				tagbug_cornerRadius = 0;
				tagbug_outerGlow 	= settings.outerGlow;
				tagbug_borderWidth 	= settings.borderWidth; 
				tagbug_borderColor 	= settings.borderColor;
				tagbug_bgTopColor 	=  settings.bgTopColor;
				tagbug_bgButColor	= settings.bgButColor;
				tagbug_buttonColor 	= settings.buttonColor;
				tagbug_textName 	= settings.textName;
				tagbug_textColor	= settings.textColor;
				
				//calculate the width of the iconline
				var tagbug_iconline_width = (response.count*30) - 1;
				
				jQuery('#tagbug_popupWidget div').css("display","block");
				
				jQuery("#tagbug_popupWidget").css("width",tagbug_width);
				jQuery("#tagbug_popupWidget").css("height",tagbug_height);
				
				jQuery("#tagbug_popupWidget").css("border",tagbug_borderWidth+"px solid "+tagbug_borderColor);
				jQuery('#tagbug_popupWidget').gradient({
					from: tagbug_bgTopColor.substring(1), 
					to: tagbug_bgButColor.substring(1),
					direction: 'horizontal',
					length: tagbug_height
				});
				
				jQuery('#tagbug_popupWidget div').css("display","block");
				jQuery('#tagbug_popupWidget #tagbug_ul1 a').css("color",tagbug_textColor);
				jQuery('#tagbug_popupWidget #tagbug_ul1 a').css("font-family",tagbug_textName);
				
				//LOADING POPUP
				
				//Click the button event!
				jQuery("#tagbug_logoLink").click(function(){
					//centering with css
					tagbug_centerPopupWidget();
					//load popup
					tagbug_loadPopupWidget();
				});
				
				jQuery("#tagbug_icoLine").hide();
				var mouseoutTimer = null;
				jQuery("#tagbug_logoLink").mouseover(function(){
					//jQuery("#tagbug_icoLine").animate({"width": tagbug_iconline_width+"px"},100);
					jQuery("#tagbug_icoLine").animate({"width": "show"},100);
					clearTimeout(mouseoutTimer);
				});
				
				jQuery("#tagbug_icoLine").mouseover(function(){
					clearTimeout(mouseoutTimer);
				});
				
				jQuery("#tagbug_logoLink, #tagbug_icoLine").mouseout(function(){
				       mouseoutTimer = setTimeout(function(){
				    	   //jQuery("#tagbug_icoLine").animate({"width": "0px"},100);
				    	   jQuery("#tagbug_icoLine").animate({"width": "hide"},100);
				       },100);
				});
				
				//CLOSING POPUP
				//Click the x event!
				jQuery("#tagbug_popupWidgetClose").click(function(){
					tagbug_disablePopupWidget();
				});
				//Click out event!
				jQuery("#tagbug_backgroundPopupWidget").click(function(){
					tagbug_disablePopupWidget();
				});
			}
	);
	
	//Press Escape event!
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupWidgetStatus==1){
			tagbug_disablePopupWidget();
		}
	});
	
});
