/* widget controls */

function add(id,column){
	
	$('<li class="widget color-blue" id="'+id+'"><div class="widget-head"><h3>'+id+'</h3></div><div class="widget-content"><div id="data_'+id+'">Loading...</div></div></li>').appendTo('#'+column);	

	$('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) {
		e.stopPropagation();    
		}).click(function () {
			if(confirm('This widget will be removed, ok?')) {
				$('#'+id).animate({
					opacity: 0    
				},function () {
					$('#'+id).wrap('<div/>').slideUp(function () {
						$('#'+id).remove();
						checkchange();
					});
				});
			}
			return false;
		}).appendTo($('.widget-head', '#'+id));

		$('<a href="#" class="collapse">COLLAPSE</a>').mousedown(function (e) {
			e.stopPropagation();    
		}).toggle(function () {
			$(this).css({backgroundPosition: '-38px 0'})
				.parents('.widget')
					.find('.widget-content').hide();
			return false;
		},function () {
			$(this).css({backgroundPosition: ''})
				.parents('.widget')
					.find('.widget-content').show();
			return false;
		}).prependTo($('.widget-head','#'+id));

        $('.column').find('.widget-head').css({
            cursor: 'move'
        }).mousedown(function (e) {
            $('.column').css({width:''});
            $(this).parent().css({
                width: $(this).parent().width() + 'px'
            });
        }).mouseup(function () {
            if(!$(this).parent().hasClass('dragging')) {
                $(this).parent().css({width:''});
            } else {
                $('.column').sortable('disable');
            }
        });
		
		getcontents(id);
           
}

function add_user_wid(xid){

	$.post('/assets/scripts/get_col_mpg.php',"xid="+xid,function (data){
		if(data.status){
			var c1 = new Array();
			var c2 = new Array();
			c1 = data.column1.split(',');
			c2 = data.column2.split(',');
			for(var i in c1){
				if(c1[i]!="")
					add(c1[i],'column1');
			}
			for(var i in c2){
				if(c2[i]!="")
					add(c2[i],'column2');
			}
		}
		else {
			add_def_wid();
		}
	},'json');

	$('.column').sortable({
            items: 'li',
            connectWith: $('.column'),
            handle: '.widget-head',
            placeholder: 'widget-placeholder',
            forcePlaceholderSize: true,
            revert: 300,
            delay: 100,
            opacity: 0.8,
            containment: 'document',
            start: function (e,ui) {
                $(ui.helper).addClass('dragging');
            },
            stop: function (e,ui) {
                $(ui.item).css({width:''}).removeClass('dragging');
                $('.column').sortable('enable');
				checkchange();
            }
	});
}

function add_def_wid(){
	
	//$.post('/assets/scripts/get_col_mpg.php',"xid="+xid,function (data){
	//});

add('news','column1');
add('sports','column2');
add('weather','column2');
add('local','column1');
add('jobs','column2');

	$('.column').sortable({
            items: 'li',
            connectWith: $('.column'),
            handle: '.widget-head',
            placeholder: 'widget-placeholder',
            forcePlaceholderSize: true,
            revert: 300,
            delay: 100,
            opacity: 0.8,
            containment: 'document',
            start: function (e,ui) {
                $(ui.helper).addClass('dragging');
            },
            stop: function (e,ui) {
                $(ui.item).css({width:''}).removeClass('dragging');
                $('.column').sortable('enable');
            }
	});
}

function checkchange(){
	var c1="";
	var c2="";
	$("ul#column1 li").each(function () {
		c1+=$(this).attr("id")+',';		
	});
	$("ul#column2 li").each(function () {
		c2+=$(this).attr("id")+',';
	});
	
	c1 = c1.substring(0, c1.length - 1);
	c2 = c2.substring(0, c2.length - 1);

	$.post('/assets/scripts/save_col_mpg.php',"c1="+c1+"&c2="+c2,function (data){
	});

}

function add2page(cat){
	var pg = $("#page").val();
	if(pg=='mypg'){
		var cc1=0;
		var cc2=0;
		var avail=0;
		$("ul#column1 li").each(function () {
			cc1++;
			if(cat==$(this).attr("id"))
				avail=1;
		});
		$("ul#column2 li").each(function () {
			cc2++;
			if(cat==$(this).attr("id"))
				avail=1;
		});
		if(avail!=1){
			if(cc1 < cc2)
				add(cat,'column1');
			if(cc1 > cc2)
				add(cat,'column2');
			if(cc1 == cc2)
				add(cat,'column1');
				
			alert('Widget added to your Page');
		} else {
			alert('Widget already available in your Page');
		}
		checkchange();
	} else {
		$.post('/assets/scripts/add_col_fop.php',"c="+cat,function (data){
			alert(data)
		});
	}
}

function getcontents(id){
	var content ;
	$.post('/assets/scripts/get_contents.php',"id="+id,function (data){
		if(data!=""){
			content = data;
		}
		else {
			content = id+" : Content unavailable at the moment, please try again later.";
		}
		$("#data_"+id).html(content);
	});
}
function getcontents_tab(id){
	var content ;
	$.post('/assets/scripts/get_contents.php',"id="+id,function (data){
		if(data!=""){
			content = data;
		}
		else {
			content = id+" : Content unavailable at the moment, please try again later.";
		}
		$("#datatab_"+id).html(content);
	});
}
/*******************************************************************************************************************/

/* function to diplay the mini contents on the div for each link on the menu */
function showpop(cat){ 
	$('html, body').animate({
	scrollTop: $("#content").offset().top
	}, 500);
	
	$("#popdiv").slideDown("slow");
	document.getElementById('pdhead').innerHTML='<span id="lk"><a href="/'+cat+'/" title="view on full page" class="view">VIEW</a>&nbsp;<a href="javascript:void(0);" class="add" title="Add to your page" onclick="add2page(\''+cat+'\');">ADD</a>&nbsp;<a href="javascript:clpdiv();" class="remove" title="close">Close Widget</a></span><span id="head1"><h2>'+cat+'</h2></span>';
document.getElementById('pdcon').innerHTML='<br><div id="datatab_'+cat+'" class="pdcontent"></div>';
	getcontents_tab(cat)
	return false;
}

function clpdiv(){
	$("#popdiv").hide();
}

function makehome(){
	$("#hpdiv").slideDown("slow");
	var brow = BrowserDetect.browser;
	if(brow=='Chrome')
		document.getElementById('hpcon').innerHTML='<i><p>From the browser menu, click the <b>Settings</b> icon<br>Select <b>Options</b><br>On the <b>Basics</b> tab, type www.mybizspace.biz in the Home Page entry box and click <b>Close</b><br></p></i>';
	else if(brow=='Firefox'){
		document.getElementById('hpcon').innerHTML="<i><ul><li>1. From the browser menu, click the <b>Tools</b><br>2. Select <b>Options</b><br>3. In the Main tab's Home Page entry box, type www.mybizspace.biz and click <b>Ok</b><br></li><li><b>Or</b> 1. drag the url and drop it onto the 'House' icon on your browser.<br> 2. Select 'Yes' from the pop up window.</li></ul></i>";
	}
	else if(brow=='Opera'){
		document.getElementById('hpcon').innerHTML='<i><p>From the browser menu, click <b>tools</b> icon<br>Select <b>Preferences</b><br>On the <b>General</b> tab, type www.mybizspace.biz in the Home Page entry box and click <b>OK</b><br></p></i>';
	}
	else if(brow=='Safari'){
		document.getElementById('hpcon').innerHTML="<i><p>From the browser menu, click the <b>Settings </b> icon<br>Select <b>Preferences</b><br>On the <b>General</b> tab, type www.mybizspace.biz in the Home Page entry box and you're done!<br></p></i>";
	}
}

function clhdiv(){
	$("#hpdiv").hide();
}
/************************* Other functions ******************/
//horoscope functions
function savesign_hor(){
	var sign = $("#horselect").val();
	$.post('/assets/scripts/save_hor_sign.php',"s="+sign,function (data){
		alert(data);
		var pg = $("#page").val();
		if(pg=='horoscope')
			$(location).attr('href','/horoscope/');
		else if(pg=='mypg')
			$(location).attr('href','/mypage/');
		else
			showpop('horoscope');
	});
	
}

function oc(div){
	if($("#"+div).css("display")=='none'){
		$("#"+div).slideDown("fast");
	} else {
		$("#"+div).hide();
	}
	return false;
}
//