(function() {
	var Dom = YAHOO.util.Dom;
	var Event = YAHOO.util.Event;
	var DDM = YAHOO.util.DragDropMgr;
	
	YAHOO.example.DDApp = {
		init: function() {

			new YAHOO.util.DDTarget("gp-1");
			new YAHOO.util.DDTarget("gp-2");
			new YAHOO.util.DDTarget("gp-3");
			new YAHOO.util.DDTarget("gp-4");			

			for(var i=1;i<18;i++)
			{
				var mod1 = new YAHOO.example.DDdiv("md" + i);
				mod1.setHandleElId("hmd" + i);
			}				
		}
	};

	//////////////////////////////////////////////////////////////////////////////
	// custom drag and drop implementation
	//////////////////////////////////////////////////////////////////////////////

	YAHOO.example.DDdiv = function(id, sGroup, config) {

		YAHOO.example.DDdiv.superclass.constructor.call(this, id, sGroup, config);
		var el = this.getDragEl();

		Dom.setStyle(el, "opacity", 0.37); // The proxy is slightly transparent

		this.goingUp = false;
		this.lastY = 0;
	};



	YAHOO.extend(YAHOO.example.DDdiv, YAHOO.util.DDProxy, {
		startDrag: function(x, y) {

		   // make the proxy look like the source element
			var dragEl = this.getDragEl();
			var clickEl = this.getEl();
			Dom.setStyle(clickEl, "visibility", "hidden");
			
			dragEl.innerHTML = clickEl.innerHTML;

			Dom.setStyle(dragEl, "color", Dom.getStyle(clickEl, "color"));
			Dom.setStyle(dragEl, "backgroundColor", Dom.getStyle(clickEl, "backgroundColor"));
			Dom.setStyle(dragEl, "border", "2px solid gray");
		},

		endDrag: function(e) {

			var srcEl = this.getEl();
			var proxy = this.getDragEl();

			// Show the proxy element and animate it to the src element's location
			Dom.setStyle(proxy, "visibility", "");

			var a = new YAHOO.util.Motion( 
				proxy, { 
					points: { 
						to: Dom.getXY(srcEl)
					}
				}, 
				0.2, 
				YAHOO.util.Easing.easeOut 
			)
			var proxyid = proxy.id;
			var thisid = this.id;
			

			// Hide the proxy and show the source element when finished with the animation
			a.onComplete.subscribe(function() {
					Dom.setStyle(proxyid, "visibility", "hidden");
					Dom.setStyle(thisid, "visibility", "");
				});
			a.animate();
			
			proxy.innerHTML="";
			
			change_height();

			if(login_message==0)
			{
				lightboxon('lb-login');
				login_message=1;
			}

			if(user_login==1)
			{
				save_modules();				
			}
		},

		onDragDrop: function(e, id) {

			// If there is one drop interaction, the li was dropped either on the list,
			// or it was dropped on the current location of the source element.
			if (DDM.interactionInfo.drop.length === 1) {

				// The position of the cursor at the time of the drop (YAHOO.util.Point)
				var pt = DDM.interactionInfo.point; 

				// The region occupied by the source element at the time of the drop
				var region = DDM.interactionInfo.sourceRegion; 

				// Check to see if we are over the source element's location.  We will
				// append to the bottom of the list once we are sure it was a drop in
				// the negative space (the area of the list without any list items)
				if (!region.intersect(pt)) {
					var destEl = Dom.get(id);
					var destDD = DDM.getDDById(id);
					destEl.appendChild(this.getEl());
					destDD.isEmpty = false;
					DDM.refreshCache();
				}
			}
		},

		onDrag: function(e) {

			// Keep track of the direction of the drag for use during onDragOver
			var y = Event.getPageY(e);

			if (y < this.lastY) {
				this.goingUp = true;
			} else if (y > this.lastY) {
				this.goingUp = false;
			}

			this.lastY = y;
		},

		onDragOver: function(e, id) {
		
			var srcEl = this.getEl();
			var destEl = Dom.get(id);

			// We are only concerned with list items, we ignore the dragover
			// notifications for the list.
		   // if (destEl.nodeName.toLowerCase() == "div") {
		  // alert(destEl.nodeName);

		   if ((destEl.className.toLowerCase() == "md") && (destEl.nodeName.toLowerCase() == "div")) {
				
				var orig_p = srcEl.parentNode;
				var p = destEl.parentNode;

				if (this.goingUp) {
					p.insertBefore(srcEl, destEl); // insert above
				} else {
					p.insertBefore(srcEl, destEl.nextSibling); // insert below
				}

				DDM.refreshCache();
			}
		}
	});

	Event.onDOMReady(YAHOO.example.DDApp.init, YAHOO.example.DDApp, true);

	})();

	
function change_height()
{
	document.getElementById("gp-1").style.height = document.getElementById("gp-2").style.height = document.getElementById("gp-3").style.height = document.getElementById("gp-4").style.height = 'auto';

	var gp1_height = document.getElementById("gp-1").offsetHeight;
	var gp2_height = document.getElementById("gp-2").offsetHeight;
	var gp3_height = document.getElementById("gp-3").offsetHeight;
	var gp4_height = document.getElementById("gp-4").offsetHeight;

	var max_height1 = Math.max(gp1_height, gp2_height, gp4_height);

	document.getElementById("gp-1").style.height=max_height1 + 'px';
	document.getElementById("gp-2").style.height=max_height1 + 'px';
	document.getElementById("gp-4").style.height=max_height1 + 'px';
}

function save_modules()
{
	
	var Dom = YAHOO.util.Dom;
	var parseList = function()
	{
		var items = Dom.getElementsByClassName("md", "div");
		var out = "";
		for (var i=0;i<items.length;i=i+1) {					
			out += document.getElementById(items[i].id).parentNode.id + "," + items[i].id + "|";
		}
		return out;
	};

	var user_modules=parseList();
	
	var postData = 'user_modules='+ user_modules;
	var handleSuccess = function(o){}
	var handleFailure = function(o){}
	var Callback ={ success:handleSuccess,failure: handleFailure};

	 YAHOO.util.Connect.asyncRequest("POST", '/save_modules.php', Callback, postData);
}

var show_tabs= function(id)
{
         YAHOO.util.Event.onDOMReady(function() { 

				var id1=0;
				for(var i=1; i<=7; i++)
				{
					var temp="global-tab-name-"+i;
					if(document.getElementById(temp).className=='tab-active')
					{
						id1=i;
						break;
					}
				}

				if(id!=id1)
				{
					var global_tab="global-tab-"+id;
					new YAHOO.widget.Effects.Show(global_tab);
					
					var global_tab_name="global-tab-name-"+id;
					YAHOO.util.Dom.addClass(global_tab_name, 'tab-active'); 
										
					var global_tab1="global-tab-"+id1;
					new YAHOO.widget.Effects.Hide(global_tab1);
					
					var global_tab_name1="global-tab-name-"+id1;
					YAHOO.util.Dom.removeClass(global_tab_name1, 'tab-active'); 
				}
	             
	        }); 
 
	
}



function show_channel_news(channel_id, channel_name, n)
{
	var cdiv='inner-news';
	var postdata = 'channel_id='+ channel_id + '&news_no=' + n ;
	var div = document.getElementById(cdiv);

	var handle = "hmd10";
	document.getElementById(handle).style.width = 'auto';
	var width = document.getElementById(handle).offsetWidth;
	width = width - 2;

	div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0 35px;width:' + width +'px"><center>Loading...<br /><img src="/css-img/loading.gif" /></center></div>';

	var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText; change_height();}}
	var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
	var Callback ={success:handleSuccess,failure: handleFailure};

	 YAHOO.util.Connect.asyncRequest("POST", "/inner_newsroom.php", Callback, postdata);

	document.getElementById('ch-name').innerHTML = channel_name;
	document.getElementById('top').style.display = 'none';

}


function show_cj_news(continent_name,continent_id, n)
{
	var cdiv='inner-news2';
	var postdata = 'continent_id='+ continent_id + '&news_no=' + n ;
	var div = document.getElementById(cdiv);

	var handle = "hmd2";
	document.getElementById(handle).style.width = 'auto';
	var width = document.getElementById(handle).offsetWidth;
	width = width - 2;

	div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0 35px;width:' + width +'px"><center>Loading...<br /><img src="/css-img/loading.gif" /></center></div>';

	var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText; change_height();}}
	var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
	var Callback ={success:handleSuccess,failure: handleFailure};

	 YAHOO.util.Connect.asyncRequest("POST", "/inner_cj.php", Callback, postdata);

	document.getElementById('ch-name1').innerHTML = continent_name;
	document.getElementById('top1').style.display = 'none';

}

function save_channel_module(md_no ,tag_id)
{

	var form = "md" + md_no + "_articles";
	var oForm = document.getElementById(form);
	var cdiv='module' + md_no + "-body";
	var postdata = 'md_no='+ md_no + '&n_articles=' + oForm.elements['option'].value + '&tag_id=' + tag_id;
	var div = document.getElementById(cdiv);

	var handle = "hmd" + md_no;
	document.getElementById(handle).style.width = 'auto';
	var width = document.getElementById(handle).offsetWidth;
	width = width - 2;

	div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0 35px;width:' + width +'px"><center>Loading...<br /><img src="/css-img/loading.gif" /></center></div>';

	var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;change_height();}}
	var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
	var Callback ={success:handleSuccess,failure: handleFailure};

	 YAHOO.util.Connect.asyncRequest("POST", "/save_channel_settings.php", Callback, postdata);
}

function save_comment_module(md_no)
{

	var form = "md" + md_no + "_articles";
	var oForm = document.getElementById(form);
	var cdiv='module' + md_no + "-body";
	var postdata = 'n_articles=' + oForm.elements['option'].value;
	var div = document.getElementById(cdiv);

	var handle = "hmd" + md_no;
	document.getElementById(handle).style.width = 'auto';
	var width = document.getElementById(handle).offsetWidth;
	width = width - 2;

	div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0 35px;width:' + width +'px"><center>Loading...<br /><img src="/css-img/loading.gif" /></center></div>';

	var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;change_height();}}
	var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
	var Callback ={success:handleSuccess,failure: handleFailure};

	 YAHOO.util.Connect.asyncRequest("POST", "/save_comment_settings.php", Callback, postdata);
}

function save_nif_module(md_no)
{

	var form = "md" + md_no + "_articles";
	var oForm = document.getElementById(form);
	var cdiv='module' + md_no + "-body";
	var postdata = 'md_no='+ md_no + '&n_articles=' + oForm.elements['option'].value;
	var div = document.getElementById(cdiv);

	var handle = "hmd" + md_no;
	document.getElementById(handle).style.width = 'auto';
	var width = document.getElementById(handle).offsetWidth;
	width = width - 2;

	div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0 35px;width:' + width +'px"><center>Loading...<br /><img src="/css-img/loading.gif" /></center></div>';

	var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;change_height();}}
	var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
	var Callback ={success:handleSuccess,failure: handleFailure};

	 YAHOO.util.Connect.asyncRequest("POST", "/save_nif_settings.php", Callback, postdata);
}

function change_country(selected_value,divid)
{

		var div = document.getElementById(divid);
		var div1 = document.getElementById('country1');	
		var div2 = document.getElementById('country2');
		
		var temp = new Array();	
		temp = selected_value.split('|');
		var postdata ='selected_value=' + temp[0] +'&country_name=' + temp[1];
	
		div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding-top:50px;color:#777;height:220px;padding-top:"><center><img src="/css-img/wheel.gif" /> &nbsp; &nbsp;Getting news from&nbsp;<span style="color:#000;">'+temp[1] +'</span></center></div>';
		div1.innerHTML = temp[1];
		div2.innerHTML = '<a href="http://www.instablogs.com/'+ temp[0] + '/">' + temp[1] + '</a><a href="/feed/'+ temp[0] + '/">&nbsp;<img src="/css-img/md-rss.gif"></a>';

		var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;}}
		var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
		var Callback ={success:handleSuccess,failure: handleFailure};

		var request = YAHOO.util.Connect.asyncRequest("POST", '/change_global_country.php', Callback, postdata);

		recordstat('gss-change-country',temp[0],'0','0', 'click');
}



function save_newsroom_module(md_no)
{

	var form = "md" + md_no + "_articles";
	var oForm = document.getElementById(form);
	var cdiv='module' + md_no + "-body";
	var postdata = 'md_no='+ md_no + '&n_articles=' + oForm.elements['option'].value + '&channel_no=' + oForm.elements['channel_no'].value;
	var div = document.getElementById(cdiv);

	var handle = "hmd" + md_no;
	document.getElementById(handle).style.width = 'auto';
	var width = document.getElementById(handle).offsetWidth;
	width = width - 2;

	div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0 35px;width:' + width +'px"><center>Loading...<br /><img src="/css-img/loading.gif" /></center></div>';

	var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;change_height();}}
	var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
	var Callback ={success:handleSuccess,failure: handleFailure};

	YAHOO.util.Connect.asyncRequest("POST", "/save_newsroom_settings.php", Callback, postdata);
}


function save_cj_module(md_no)
{

	var form = "md" + md_no + "_articles";
	var oForm = document.getElementById(form);
	var cdiv='module' + md_no + "-body";
	var postdata = 'md_no='+ md_no + '&n_articles=' + oForm.elements['option'].value;
	var div = document.getElementById(cdiv);

	var handle = "hmd" + md_no;
	document.getElementById(handle).style.width = 'auto';
	var width = document.getElementById(handle).offsetWidth;
	width = width - 2;

	div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0 35px;width:' + width +'px"><center>Loading...<br /><img src="/css-img/loading.gif" /></center></div>';

	var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;change_height();}}
	var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
	var Callback ={success:handleSuccess,failure: handleFailure};

	YAHOO.util.Connect.asyncRequest("POST", "/save_cj_settings.php", Callback, postdata);
}


function save_viewpoint_module(md_no)
{

	var form = "md" + md_no + "_articles";
	var oForm = document.getElementById(form);
	var cdiv='module' + md_no + "-body";
	var postdata = 'md_no='+ md_no + '&n_articles=' + oForm.elements['option'].value;
	var div = document.getElementById(cdiv);

	var handle = "hmd" + md_no;
	document.getElementById(handle).style.width = 'auto';
	var width = document.getElementById(handle).offsetWidth;
	width = width - 2;

	div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0 35px;width:' + width +'px"><center>Loading...<br /><img src="/css-img/loading.gif" /></center></div>';

	var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;change_height();}}
	var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
	var Callback ={success:handleSuccess,failure: handleFailure};

	YAHOO.util.Connect.asyncRequest("POST", "/save_viewpoint_settings.php", Callback, postdata);
}


function save_local_module(md_no)
{

	var form = "md" + md_no + "_articles";
	var oForm = document.getElementById(form);
	var cdiv='module' + md_no + "-body";
	var postdata = 'md_no='+ md_no + '&n_articles=' + oForm.elements['option'].value + '&temp_location=' + oForm.elements['local'].value;
	var div = document.getElementById(cdiv);

	var handle = "hmd" + md_no;
	document.getElementById(handle).style.width = 'auto';
	var width = document.getElementById(handle).offsetWidth;
	width = width - 2;

	div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0 35px;width:' + width +'px"><center>Loading...<br /><img src="/css-img/loading.gif" /></center></div>';

	var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;change_height();}}
	var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
	var Callback ={success:handleSuccess,failure: handleFailure};

	YAHOO.util.Connect.asyncRequest("POST", "/save_location_settings.php", Callback, postdata);
}

function trash(divid,postid,user_id) 
	{
		var div = document.getElementById(divid);	
		var postdata ='postid='+postid + '&user_id=' + user_id;
		new YAHOO.widget.Effects.Fade(div);

		var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;}}
		var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
		var Callback ={success:handleSuccess,failure: handleFailure};

		var request = YAHOO.util.Connect.asyncRequest("POST", '/update_trash.php', Callback,postdata);
	}

function show_top_tabs(divid,divid1)
	{

		var div = document.getElementById(divid);
		var div1 = document.getElementById(divid1);
		if(div.style.display == 'block' || div.style.display == '')
		{
			new YAHOO.widget.Effects.Show(divid1);
			new YAHOO.widget.Effects.Hide(divid);
		}
		else
		{
			new YAHOO.widget.Effects.Show(divid);
			new YAHOO.widget.Effects.Hide(divid1);
		}
	}

	function minimize_all()
	{
		if(login_message==0)
		{
			lightboxon('lb-login');
			login_message=1;
		}


		for(var i=1; i<18; i++)
		{
			var divinner='module' + i + "-body";
			var ch_arrow='arrow' + i;
			var cdiv=document.getElementById(divinner);
			var div=document.getElementById(ch_arrow);

			if(div!=null && cdiv!=null)
			{
				new YAHOO.widget.Effects.Hide(cdiv);
				div.innerHTML='<img src="/css-img/arrow-rt.gif" >';
			}
		}

		change_height();
	}


	function maximize_all()
	{
		if(login_message==0)
		{
			lightboxon('lb-login');
			login_message=1;
		}
		
		for(var i=1; i<18; i++)
		{
			var divinner='module' + i + "-body";
			var ch_arrow='arrow' + i;
			var cdiv=document.getElementById(divinner);
			var div=document.getElementById(ch_arrow);

			if(div!=null && cdiv!=null)
			{
				new YAHOO.widget.Effects.Show(cdiv);
				div.innerHTML='<img src="/css-img/arrow.gif" >';
			}
		}

		change_height();
	}

	function cj_paging(page_no, total_pages, total_articles, divid)
	{
		var div = document.getElementById(divid);
		var height = div.offsetHeight;
		var postdata ='page_no='+ page_no + '&total_pages=' + total_pages + '&total_articles=' + total_articles;
		div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0;text-align:center;height:' + height +'px"><img src="/css-img/wheel.gif" /> &nbsp; &nbsp;Loading...</div>';

		var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;}}
		var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
		var Callback ={success:handleSuccess,failure: handleFailure};

		var request = YAHOO.util.Connect.asyncRequest("POST", '/cj_home_paging.php', Callback, postdata);

		recordstat('cj-page','home','0','0', 'click');
	}

	function club_paging(page_no, total_pages, total_articles, divid, channel_id)
	{
		var div = document.getElementById(divid);
		var height = div.offsetHeight;
		var postdata ='page_no='+ page_no + '&total_pages=' + total_pages + '&total_articles=' + total_articles + '&channel_id=' + channel_id;
		div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0;text-align:center;height:' + height +'px"><img src="/css-img/wheel.gif" /> &nbsp; &nbsp;Loading...</div>';

		var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;}}
		var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
		var Callback ={success:handleSuccess,failure: handleFailure};

		var request = YAHOO.util.Connect.asyncRequest("POST", '/club_home_paging.php', Callback, postdata);
	}

	function newsroom_paging(page_no, total_pages, total_articles, divid, channel_id)
	{
		var div = document.getElementById(divid);
		var height = div.offsetHeight;
		var postdata ='page_no='+ page_no + '&total_pages=' + total_pages + '&total_articles=' + total_articles + '&channel_id=' + channel_id;
		div.innerHTML = '<div style="font:bold 1.2em/1.2em Georgia, serif;padding:20px 0;text-align:center;height:' + height +'px"><img src="/css-img/wheel.gif" /> &nbsp; &nbsp;Loading...</div>';

		var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;}}
		var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
		var Callback ={success:handleSuccess,failure: handleFailure};

		var request = YAHOO.util.Connect.asyncRequest("POST", '/newsroom_home_paging.php', Callback, postdata);

		recordstat('club-page','home','0','0', 'click');
	}


	function show_ap_vid_page(page_no, total_vids, divid) 
	{
		var div = document.getElementById(divid);

		var postdata ="page_no="+page_no+"&total_vids="+total_vids;

		div.innerHTML = '<div class="loading-page-wheel"><center><img style="border:none; padding:20px 0" src="/css-img/wheel.gif" /></center></div>';

		var handleSuccess = function(o){if(o.responseText !== undefined){div.innerHTML = o.responseText;}}
		var handleFailure = function(o){if(o.responseText !== undefined){div.innerHTML=  o.statusText;}}
		var Callback ={success:handleSuccess,failure: handleFailure};

		var request = YAHOO.util.Connect.asyncRequest("POST", 'ap_vid_paging.php', Callback, postdata);
	}
