// JavaScript Document
/***************************************************************************
/	Filename: tizit.js
/	Dev: RM
/	Created: 8.5.11
/	Purpose: Universale JS functions called throughout pages
****************************************************************************/
function siteMod(obj,e,act,cat) {
	if(act == 'del') {
	if(confirm('Are you sure you want to delete this site?')) {
		siteDel(obj,e,act,cat);
		}
	} else {
		siteEdit(obj,e,act,cat);
	}
	
}

function starit(obj, type, id, who) {
	$.ajax({
		type: 'POST',
		url: 'lib/controls.wall.php',
		data: 'act=starit&id='+ id+'&type='+type+'&who='+who,
		success: function(data) {
			var c = $(obj).prev('span').text();
			$(obj).text('Unstar');
			$(obj).removeAttr('onclick');
			$(obj).click(function(e) {
                unstarit(obj,type,id,who);
				$(obj).unbind('click');
            });
			if(c == '' ) {
				$(obj).prev('span').html('+1<img class="star_it" title="Starred!" src="images/star.png" /> ');
			} else {
				c = c.substr(1);
				c++;
				$(obj).prev('span').html('+'+c+'<img class="star_it" title="Starred!" src="images/star.png" /> ');
			}			
		}
	});	
}

function unstarit(obj, type, id, who) {
	$.ajax({
		type: 'POST',
		url: 'lib/controls.wall.php',
		data: 'act=unstarit&id='+ id+'&type='+type+'&who='+who,
		success: function(data) {
			var c = $(obj).prev('span').text();
			$(obj).text('Star it!');
			$(obj).removeAttr('onclick');
			$(obj).click(function(e) {
                starit(obj,type,id,who);
            	$(obj).unbind('click');
			});
			c = c.substr(1);
			c--;
			if(c == 0) {
				$(obj).prev('span').html('');
			} else {
				$(obj).prev('span').html('+'+c+'<img class="star_it" title="Starred!" src="images/star.png" /> ');
			}			
		}
	});	
}

function siteDel(obj,e,act,cat) {
	$.ajax({
		type: 'POST',
		url: 'lib/site_control.php',
		data: 'site=' + e + '&act=' + act + '&cat=' + cat,
		success: function() {
			
				$(obj).closest('tr').css('background-color', 'red');
				$(obj).closest('tr').fadeOut('slow');
		
		}
	});	
}

function siteAdd(id,name) {
	$.ajax({
		type: 'POST',
		url: 'lib/site_control.php',
		data: 'site_id=' + id + '&act=add&site_name=' + name,
		success: function(data) {
			if(data == 'error') {
				alert('There was an error, please try again');
			} else if (data == 'err1') {
				alert('This site has already been added');
			} else {
				$('#bookmark_search').val('');
				$('#bookmark_search').blur();
				$('#bookmarks_finder').css('display','none');
				$('#bookmarks_content').load('lib/bookmarks.php');
				$('#current_bookmarks').load('lib/bookmarks.php');
				setTimeout('doBookmarks()',1000);
			}
		}
	});	
	
}

function siteDBUpdate(oid,id,name,cat) {
	$.ajax({
		type: 'POST',
		url: 'lib/site_control.php',
		data: 'site=' + id + '&oid=' + oid + '&act=dbup' + '&cat=' + cat + '&n=' + name,
		success: function(data) {
			if(data == 'error') {
				alert('There was an error, please try again');
			} else if (data == 'err1') {
				alert('This site has already been added');
			} else {
				$('#'+id + ' .table_title').html(name);
				$('#'+id).effect("highlight", {color : "#00ff00"}, 3000);
				cancelAll();
			}
		}
	});	
}

function siteUserUpdate(oid,name,rss,url,cat,ai) {
	$.ajax({
		type: 'POST',
		url: 'lib/site_control.php',
		data: 'site=0&oid=' + oid + '&act=userup' + '&cat=' + cat + '&n=' + name + '&url=' + url + '&rss=' +rss + '&icon=' + ai,
		success: function(data) {
			if(data == 'error') {
				alert('There was an error, please try again');
			} else {
				$('#'+oid + ' .table_title').html(name);
				$('#'+oid).effect("highlight", {color : "#00ff00"}, 3000);
				cancelAll();
			}
		}
	});		
}

function clickUserUpdate(obj, name, cat, i) {
	$('#icon_db').attr('src', 'site_icons/' + i);
	$('#edit_db_site_name').val(name);
	$('#edit_display_sites').css('display', 'none');
	$('#new_db_site').val(obj);
}

function siteEdit(obj,e,act,cat) {
	$.ajax({
			type: 'POST',
			url: 'lib/site_control.php',
			data: 'site=' + e + '&act=' + act + '&cat=' + cat,
			success: function(data) {
				/*if(data == 'comp') {
					$(obj).closest('tr').css('background-color', 'red');
					$(obj).closest('tr').fadeOut('slow');
				} else {
					alert('There was an error, please try again');
				}*/
				alert(data);
				}
		});	
}

function addSiteNotFound() {
	var ifile = $('#ifaf').contents().find('#i_file').val();
	//console.log(ifile);
	if(ifile != '') {
		$('#ifaf').contents().find('#iframe_file').submit();
		setTimeout('checkIfFileDone()',100);
	} else {
		continueAddNewSite('no');
	}
	
}

function checkIfFileDone() {
	var idone = $('#ifaf').contents().find('#ifile_move_name').text();
	if(idone != '') {
		continueAddNewSite(idone);
	} else {
		setTimeout('checkIfFileDone()',250);
	}
}

function continueAddNewSite(file) {
	if(file == 'no') {
		//console.log('adding new site');
		var ifile = '';
	} else {
		//console.log('file is there' +file);
		var ifile = file;
	}
	var an = $('#custom_bookmark_name').val();	
	var au = $('#custom_bookmark_url').val();
	var ai = $('#custom_generic_icon').val();
	au = au.replace('http://','');
	au = au.replace('https://','');
	$.ajax({
		type: 'POST',
		url: 'lib/site_control.php',
		data: 'site=0&act=anf&an=' + an + '&au=' + au+'&ai=' + ai +'&ifile=' + ifile,
		success: function(data) {
			if(data == 'error') {
				alert('There was an error, please try again');
			} else if (data == 'err1') {
				alert('This site has already been added');
			} else {
				var isrc = $('#ifaf').attr('src');
				$('#ifaf').attr('src','');
				$('#ifaf').attr('src',isrc);
				$('#add_custom_bookmark').css('display','none');
				$('#bookmarks_content').load('lib/bookmarks.php');
				$('#current_bookmarks').load('lib/bookmarks.php');
				setTimeout('doBookmarks()',1000);
			}
			
		}
	});
}

function removeCustom(e) {
	$.ajax({
		type: 'POST',
		url: 'lib/site_control.php',
		data: 'act=remcustom&cat=custom&site=' + e,
		success: function(data) {
			if(data == 'comp') {
				$(obj).closest('tr').css('background-color', 'red');
				$(obj).closest('tr').fadeOut('slow');
			} else {
				alert('There was an error, please try again');
			}
		}
	});	
}


function loadBreadcrumb() {
	$('#breadcrumb_holder').load('inc/breadcrumb.php #breadcrumb_holder');
}

function sessionExp() {
	window.location = '/index.php';
}

function slideTop() {
jQuery('html, body').animate( { scrollTop: 0 }, 'slow' );	
}

function toggleSelectBox(obj) {
	$('body').unbind('click');
	if(obj == 'pop-news') {
		$('#gtrends_select_box').css('display', 'none');
	} else {
		$('#pop-news').css('display', 'none');
	}
	var visible = $('#'+obj).css('display');
	if(visible == 'none') {
		$('#'+obj).css('display','block');
		setTimeout("bindBodyClick()", 500);
	} else {
		$('#'+obj).css('display', 'none');
		$('body').unbind('click');
	}
}

function bindBodyClick() {
	$('body').bind('click', function() {
 			// toggleSelectBox(obj);
			$('#gtrends_select_box').css('display', 'none');
			$('#pop-news').css('display', 'none');
			$('body').unbind('click');
		});
}

function fireSearch(obj) {
	var connect = $(obj).attr('id');	
	$('#searchengbox').val(connect);
	if ($('#searchgoogle').length>0) {
 		$("#searchgoogle").submit();
	} else if ($('#searchyahoo').length>0) {
		$("#searchyahoo").submit();
	} else if ($('#searchbing').length>0) {
		$("#searchbing").submit();
	} else {
		$("#searchask").submit();
	}	
}

function fireHover(obj) {
	$(obj).next('div').css('background','red');
}



function grabHeadlines() {
	for(i=0;i<5;i++) {
	var headline = $('#pop-news > span:eq('+i+') > a').html();
	var connect = $('#pop-news > span:eq('+i+') > a').attr('href');
	$('#headlines').append('<div class="news_item" alt="'+headline+'" title="'+headline+'"><a href="'+connect+'" target="_blank">'+headline+'</a></div>');
	}
}

function grabKeywordHeadlines() {
	var keyword = $('#gtrends_select_box > span:eq(0)').attr('id');
	$('#keyword_headline').append('<div class="news_item" alt="'+keyword+'" title="'+keyword+'"><a  id="'+keyword+'" onclick="fireSearch(this);">'+keyword+'</a></div>');
	
}

function scrollMaker() {
	$('.content_scroll').slimScroll({
		height: '100%'
	});
}

function approveRequest(obj, e, i){
	var group = $('#type_' + i).val();
	$.ajax({
		type: 'POST',
		url: 'lib/controls.user.php',
		data: 'act=approvereq&id=' + e + '&group=' + group,
		success: function(data) {
				if(data > 0) {
					$(obj).closest('tr').fadeOut('slow');	
					var countleft = $('#friendBox > table > tr').length;
					if(countleft == 0) {
						$('#friend_notify_box').html('No pending friend requests');
					}
					$('#people_content').load('lib/friends.php');
					//alert(countleft);
				} else {
					alert('An error occurred, please try again');
				}
		}
		});		
}

function ignoreRequest(obj, e){
	$.ajax({
		type: 'POST',
		url: 'lib/controls.user.php',
		data: 'act=ignorereq&id=' + e,
		success: function(data) {
				if(data > 0) {
					$(obj).closest('tr').fadeOut('slow');
				} else {
					alert('An error occurred, please try again');
				}
		}
		});		
}
/***************************************************************************
/Widget functions
***************************************************************************/
function loadWidget(obj) {
	var widget = $(obj).attr('id');
	var widget_array = widget.split('_');
	var name = widget_array[1];
	$(obj).bind('click', function(event) {
		event.preventDefault();
		//console.log('preventing click');
	});
	if($('#widget_'+name).length > 0) {
		alert('You already have that widget on this page!');	
	} else {
		$(obj).children('.title').html('<img style="display:block; float:left;" src="images/check.png"/><span style="color:#bae86a; font-size:.9em; display:block; float:left;"> Added!</span>');
		$.ajax({
		type: 'POST',
		url: 'lib/controls.widgets.php',
		data: 'act=add&page=tizit&widget=' + name,
		success: function(data) {
			if(data == 1) {
				//Load the widget into the frame
				var frame = '<div id="widget_'+name+'" class="widget_item" style="display:none;"></div>';
				var loader = '../widgets/'+name+'.php?q=';
				$('#all_widgets').append(frame);
				$('#widget_'+name).load(loader);
				$('#widget_'+name).fadeIn('slow');
				$('#add_widgets_here').css('display','none');
				widgetMove();
				setTimeout('resizeWindows()', 1000);
			}
		}
	});
	}
}

function widgetMove() {
	$('#all_widgets').sortable({
		handle: '.header',
		stop: function(event, ui) {
			var update = '';
			$('.widget_item').each(function(index, element) {
                var id = $(this).attr('id');
				id = id.split('_');
				update += id[1] + '#';
            });	
			var page = $('#page_id').text();
			$.ajax({
				type: 'POST',
				url: 'lib/controls.widgets.php',
				data: 'act=order&page=tizit&order='+escape(update),
				success: function(data) {
					//console.log(data);
				}
			});	
		}
	});
}

function widgetLoader() {
	$('.widget_item').each(function(index, element) {
        var widget = $(this).text();
		var id = $(this).attr('id');
		$('#'+id).load('../widgets/'+widget, function(response, status, xhr) {
			if(status == 'success') {
				$('#'+id).slideDown('slow');
			} else {
				$('#'+id).html('<div class="alert-message-info"><p><strong>Whoops!</strong> Widget didn\'t load. Click "Refresh" to try again.</p></div>');
				$('#'+id).slideDown('slow');
			}
		});
    });	
	widgetMove();
}

function killWidget(widget) {
	$.ajax({
		type: 'POST',
		url: 'lib/controls.widgets.php',
		data: 'act=remove&widget='+widget,
		success: function(data) {
			$('#widget_'+widget).css('float','left');
			$('#widget_'+widget).fadeOut('slow', function() {
				$('#widget_'+widget).remove();
				$('#center').css('min-height','700px');
				setTimeout('resizeWindows()', 1000);
			});
		}
	});		
}

function widgetUpdate(widget,opts) {
	var page = $('#page_id').text();
	$.ajax({
			type: 'POST',
			url: 'lib/controls.widgets.php',
			data: 'act=update&page='+page+'&widget='+widget+'&options='+escape(opts),
			success: function(data) {
				if(data == 1) {
					return true;
				} else {
					return false;
				}
			}
	});		
}

function widgetAppend(widget,opts) {
	var page = $('#page_id').text();
	$.ajax({
			type: 'POST',
			url: 'lib/controls.widgets.php',
			data: 'act=append&page='+page+'&widget='+widget+'&options='+escape(opts),
			success: function(data) {
				if(data == 1) {
					return true;
				} else {
					return false;
				}
			}
	});		
}

function widgetRemoveOption(widget,opts) {
	var page = $('#page_id').text();
	$.ajax({
			type: 'POST',
			url: 'lib/controls.widgets.php',
			data: 'act=remopt&page='+page+'&widget='+widget+'&options='+escape(opts),
			success: function(data) {
				if(data == 1) {
					return true;
				} else {
					return false;
				}
			}
	});		
}

/***************************************************************************
/Block functions
***************************************************************************/
function blockUp(obj) {
	//Jump up a size
	//Check to see where it's at
	var block = $(obj).parents().parent().children('.content').css('display');
	//console.log(block);
	if(block == 'none') {
		$(obj).parent().parent().children('.content').slideDown('fast');
	} else {
		var this_block = $(obj).parent().parent().children('.content');
		$("#tab_content .block .content").each(function(index, element) {
            $(this).css('display','none');
        });
		$(obj).parent().parent().children('.content').css('display','block');
		$(obj).parent().parent().children('.content').animate({'height':'100%'},'fast');
	}
}

function blockDown(obj) {
	//Jump down a size
	var block = $(obj).parents().parent().children('.content').css('display');
	var h_block = $(obj).parents().parent().children('.content').css('height');
	if(block == 'block') {
		if(h_block != '275px') {
			$(obj).parent().parent().children('.content').animate({'height':'275px'},'fast');
		} else {
			$(obj).parent().parent().children('.content').slideUp('fast');
		}
	}
}

function killContent(con,obj) {
	var id = $('#center_tabs li.active').attr('id');
	$.ajax({
			type: 'POST',
			url: 'lib/controls.user.php',
			data: 'act=content_remove&id='+id+'&content='+escape(con),
			success: function(data) {
				$(obj).parent().parent().fadeOut('fast',function() {
					$(this).remove();
				});
			}
		});
}

function blockMove() {
	$('#tab_content').sortable({
		handle: '.social_image',
		stop: function(event, ui) {
			var update = '';
			$('#tab_content .block').each(function(index, element) {
                var id = $(this).attr('id');
				id = id.split('_');
				update += id[1] + '#';
            });	
			var tab = $('#center_tabs li.active').attr('id');
			$.ajax({
				type: 'POST',
				url: 'lib/controls.user.php',
				data: 'act=tab_order&id='+tab+'&content='+escape(update),
				success: function(data) {
					//console.log(data);
				}
			});	
		}
	});
}

function maximize(obj) {
	//Jump up a size
	//Check to see where it's at
	var block = $(obj).parents().parent().children('.widget_wrapper_half');
	//console.log(block);
	if($(block).css('display') == 'none') {
		$(block).slideDown('fast');
	} else {
		//var this_block = $(obj).parent().parent().children('.widget_wrapper_half');
		$("#all_widgets .widget .widget_wrapper_half").each(function(index, element) {
            $(this).css('display','none');
        });
		$(block).css('display','block');
		$(block).animate({'height':'100%'},'fast');
	}
}

function minimize(obj) {
	//Jump down a size
	var block = $(obj).parents().parent().children('.widget_wrapper_half');
	var h_block = $(obj).parents().parent().children('.widget_wrapper_half').css('height');
	if($(block).css('display') == 'block') {
		if(h_block != '300px') {
			$(block).animate({'height':'300px'},'fast');
		} else {
			$(block).slideUp('fast');
		}
	}
}

function checkLogin(obj) {
	unload = false;
	unreg = false;
	$.ajax({
		type: 'POST',
		url: 'lib/controls.user.php',
		data: 'act=unload',
		success: function() {
			$(obj).closest('form').submit();	
		}
	});	
}

function joinNow() {
	$('#login_slider').css('left','-234px');
	$('#login_form').show();
	$('#new_box').hide();
}

function doLoginForm() {
	var o = $('#login_form').css('display');
	$('#new_box').hide();
	if(o == 'none') {
		$('#login_form').show();
		$('#login_form').bind('mouseleave', function(e) {
			$('body').bind('click',function(e) {
				doLoginForm();
				$('body').unbind('click');
			});
		});
		$('#login_form').bind('mouseenter', function(e) {
			$('body').unbind('click');
		});
	} else {
		$('#login_form').hide();
		$('body').unbind('click');
		$('#login_form').unbind('mouseenter');
		$('#login_form').unbind('mouseleave');
	}
}

function needToRegister() {
	var side = $('#login_slider').css('left');
	if(side == 	'0px') {
		$('#login_slider').animate({left: '-=234'},'slow');	
	} else {
		$('#login_slider').animate({left: '+=234'},'slow');	
	}
}

/***************************************************************************
/document.ready() functions, will load last on the page
***************************************************************************/
$(document).ready(function() {
	if($(window).width() < 1250) {
		$('#top').css('position','absolute');
	}
	$(window).resize(function() {
		var wx = $(window).width();
		if(wx < 1250) {
			$('#top').css('position','absolute');	
		} else {
			$('#top').css('position','fixed');
		}
	});
});
