// SYSTEM FUNCTIONS


function notification(var_message, var_delay) {
	$.blockUI({
		message: var_message,
		timeout: var_delay,
		css: { 
			'font-family': 'Tahoma, Verdana, Arial',
			'font-size': '14px',
			width:  	'500px',
			margin:		'-50px 0 0 -250px',
			top:		'50%',
			left:   	'50%',
			right:  	'0',
			border: 	'none',
			padding:	'10px',
			opacity:	0.80,
			cursor: 	'default',
			color:		'#000000',
			backgroundColor: '#FFFFFF',
			'-webkit-border-radius': '10px',
			'-moz-border-radius':	 '10px',
			'border-radius': 		 '10px'
		} 
	});
}

function method_json(var_url) {
	$.post(var_url,
		function(data){
			var var_response_method = data.method;			// METHOD ID
			var var_response_flag = data.flag;				// VARIABLE
			var var_response_element = data.element;		// DOCUMENT ELEMENT TO MODIFY
			var var_response_value = data.value;			// RESPONSE VALUE
			var var_response_title = data.title;			// RESPONSE TITLE
			var var_response_message = data.message;		// RESPONSE MESSAGE
			var var_response_reload = data.reloadpage;		// RESPONSE MESSAGE
			var var_response_delay = data.delay;			// RESPONSE MESSAGE
			var var_response_exec_start = data.exec_start;	// RESPONSE EXEC START
			var var_response_exec_end = data.exec_end;		// RESPONSE EXEC END
			
			method_notification(var_response_message, var_response_reload, var_response_delay);
			
			if(var_response_value == 2) {
				jQuery('#jdimmer').fadeOut();
			}
			
		}, 'json');
}

function method_notification(var_message, var_reload, var_delay) {
	if(var_reload) {
		try{
			$.blockUI({
				message: var_message,
				css: {
					'font-family': 'Tahoma, Verdana, Arial',
					'font-size': '14px',
					width:  	'500px',
					margin:		'-50px 0 0 -250px',
					top:		'50%',
					left:   	'50%',
					right:  	'0',
					border: 	'none',
					padding:	'10px',
					opacity:	0.80,
					cursor: 	'default',
					color:		'#000000',
					backgroundColor: '#FFFFFF',
					'-webkit-border-radius': '10px',
					'-moz-border-radius':	 '10px',
					'border-radius': 		 '10px'
				},
				onUnblock: function() {
					$('#method').html("<script type='text/javascript'>window.location.reload();</script>");	
				}
			});
		} catch(err) {
				
		}
	} else {
		$.blockUI({
			message: var_message,
			timeout: var_delay,
			css: { 
				'font-family': 'Tahoma, Verdana, Arial',
				'font-size': '14px',
				width:  	'500px',
				margin:		'-50px 0 0 -250px',
				top:		'50%',
				left:   	'50%',
				right:  	'0',
				border: 	'none',
				padding:	'10px',
				opacity:	0.80,
				cursor: 	'default',
				color:		'#000000',
				backgroundColor: '#FFFFFF',
				'-webkit-border-radius': '10px',
				'-moz-border-radius':	 '10px',
				'border-radius': 		 '10px'
			} 
		});
	}

}

// PAGE FUNCTIONS

function page_dimmer_open(var_title, var_preview, var_download, var_target) {
	
	var var_js1 = "jQuery('#jdimmer').fadeOut();";
	if(var_target == "" || var_target == "lightbox") {
		var_js1 = "jlightbox_popup('" + var_preview + "');" + var_js1;
	} else {
		var_js1 = "location.href = '" + var_preview + "';"+ var_js1;
	}
	var var_js2 = "window.open('" + var_download + "'); jQuery('#jdimmer').fadeOut();";
    var var_onclick1 = new Function(var_js1);
	var var_onclick2 = new Function(var_js2);

	$("#jdimmer_open_title").html("<span class='text_darkgrey'>View</span> "+var_title);
    $("#jdimmer_open_link1").unbind().click(null).click(var_onclick1);
    $("#jdimmer_open_link2").unbind().click(null).click(var_onclick2);
	
	center_divs();
	jQuery('#jdimmer_content').hide();
	jQuery('#jdimmer_open').show();
	jQuery('#jdimmer').fadeIn();
	
}

function page_dimmer(var_type, var_params, var_width, var_height) {

	var var_url = '/common.php?type='+var_type+'&'+var_params;

	if(var_params != '') {
		var_url = var_url+'&'+var_params;
	}

	if(var_type == '') {
		var_url = '';
	} else if(var_type == 'message') {
		var_width = 475;
		var_height = 200;
	} else if(var_type == 'text') {
		var_width = 475;
		var_height = 200;
	}

	if(var_url != '') {
		if(var_width != null) {
			if(var_width < 0) {
				jQuery('#jdimmer_content').css("width", (var_width*-1)+"%");
			} else {
				jQuery('#jdimmer_content').css("width", var_width+"px");
			}
		}
		if(var_height != null) {
			if(var_height < 0) {
				jQuery('#jdimmer_content').css("height", (var_height*-1)+"%");
			} else {
				jQuery('#jdimmer_content').css("height", var_height+"px");
			}
		}
		center_divs();
		jQuery('#jdimmer_open').hide();
		jQuery('#jdimmer_content').show();
		jQuery('#jdimmer').fadeIn();
		if(var_type == 'text') {
			jquery_load_html('page_content_dimmer',var_params);
		} else {
			jquery_load_url('page_content_dimmer',var_url);
		}
	}
}

function page_dimmer_login() {
	page_dimmer('login','',650,315);
}

function page_dimmer_resize(var_width, var_height) {
	$('#jdimmer_content').css('width', var_width+'px').css('height', var_height+'px').center({transition:300, vertical:true, horizontal:true});
}

function page_dimmer_resize_byobject(var_object) {

	$('#'+var_object).css("margin-top", '40px');
	var var_object_width = parseInt($('#'+var_object).css("width"));
	var var_object_height = parseInt($('#'+var_object).css("height")) + 125;
	
	page_dimmer_resize(var_object_width, var_object_height);
}

function page_search(var_text, var_keycheck, e, var_pid) {
	var var_redirect = false;

	if(var_keycheck == false || var_keycheck == 'false') {
		var_redirect = true;
	} else {
		var key = (e.which) ? e.which : e.keyCode;
	
		if(key == 13) {
			var_redirect = true;
		}
	}
	
	if(var_redirect == true) {
		var var_location = "/search.php?search_type=search&search_text="+var_text;
		
		if(var_pid != null) {
			var_location = var_location + "&pid=" + var_pid;
		}
		
		top.location = var_location;
	}
}

function page_search_mobile(var_text) {
	var var_location = "/search.php?search_type=search&search_mobile=1&search_text="+var_text;
	
	top.location = var_location;
}

function page_filter(var_text,var_keycheck, e) {
	
	var var_redirect = false;

	if(var_keycheck == false || var_keycheck == 'false') {
		var_redirect = true;
	} else {
		var key = (e.which) ? e.which : e.keyCode;
	
		if(key == 13) {
			var_redirect = true;
		}
	}
	
	if(var_redirect == true) {		
		var var_url_array;
		var var_url_temp = "";
		var var_url_new = "";
		var var_url_original = document.URL;
		var var_url_params = "";
		
		var_text = url_encode(var_text);
		
		if(strpos(var_url_original, "?") !== false) {
			var_url_new = var_url_original.split("?")[0];
			var_url_temp = var_url_original.split("?")[1];
		}
		
		
		if(strpos(var_url_temp, "result_filter") !== false) {
			var_url_array = var_url_temp.split("&");
			for (var i = 0; i < var_url_array.length; i++) {
				if(var_url_params == "") {
					var_url_params += "?";	
				} else {
					var_url_params += "&";	
				}
				
				if(strpos(var_url_array[i], 'result_filter') !== false) {
					var_url_params += 'result_filter=' + var_text;
				} else {
					var_url_params += var_url_array[i];
				}
				
			}
			var_url_new += var_url_params;
		} else {
			if(var_url_temp == "") {
				var_url_temp = "?";
			} else {
				var_url_temp = "?" + var_url_temp + "&";
			}
			
			var_url_new = var_url_new + var_url_temp + 'result_filter=' + var_text;
		}
		
		top.location = var_url_new;
	}
				
	return true;
}


function page_sort(var_type) {
	
	var var_url_array;
	var var_url_temp = "";
	var var_url_new = "";
	var var_url_original = document.URL;
	var var_url_params = "";
	
	if(strpos(var_url_original, "?") !== false) {
		var_url_new = var_url_original.split("?")[0];
		var_url_temp = var_url_original.split("?")[1];
	}
	
	
	if(strpos(var_url_temp, "result_sort") !== false) {
		var_url_array = var_url_temp.split("&");
		for (var i = 0; i < var_url_array.length; i++) {
			if(var_url_params == "") {
				var_url_params += "?";	
			} else {
				var_url_params += "&";	
			}
			
			if(strpos(var_url_array[i], 'result_sort') !== false) {
				var_url_params += 'result_sort=' + var_type;
			} else {
				var_url_params += var_url_array[i];
			}
			
		}
		var_url_new += var_url_params;
	} else {
		if(var_url_temp == "") {
			var_url_temp = "?";
		} else {
			var_url_temp = "?" + var_url_temp + "&";
		}
		
		var_url_new = var_url_new + var_url_temp + 'result_sort=' + var_type;
	}
	
	top.location = var_url_new;
				
	return true;
}

function page_enterkey(var_submitname, e) {
	
	var obj = document.getElementById(var_submitname);
	var key = (e.which) ? e.which : e.keyCode;

	if(key == 13) {
		obj.click();
	}

}

function page_container_width() {
  var var_width = 0;
  
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    var_width = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    //IE 6+ in 'standards compliant mode'
    var_width = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth) ) {
    //IE 4 compatible
    var_width = document.body.clientWidth;
  }
  
  return var_width;
}

function page_container_height() {
  var var_height = 0;
  
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    var_height = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    //IE 6+ in 'standards compliant mode'
    var_height = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    //IE 4 compatible
    var_height = document.body.clientHeight;
  }
  
  return var_height;
}

function page_email_send(var_email, var_name, var_subject, var_message, var_captcha_code) {
	method_json('/includes/functions_methods.php?mtid=101&email='+var_email+'&name='+var_name+'&subject='+var_subject+'&message='+var_message+'&captcha_code='+var_captcha_code+'&sendout=1&captcha=1&json=1');	
}

// USER FUNCTIONS

function user_login(var_username, var_password, var_cookie) {
	if(var_cookie != '1') {
		var_cookie = '0';
	}
	$("#jdimmer").fadeOut(500);
	method_json('/includes/functions_methods.php?mtid=200&username='+var_username+'&password='+var_password+'&cookie='+var_cookie+'&json=1');
}

function user_logout() {
	method_json('/includes/functions_methods.php?mtid=201&json=1');
}

function user_signup(var_nick, var_email, var_username, var_password1, var_password2, var_newsletter) {
	method_json('/includes/functions_methods.php?mtid=202&nick='+var_nick+'&email='+var_email+'&username='+var_username+'&password1='+var_password1+'&password2='+var_password2+'&newsletter='+var_newsletter+'&json=1');
}

function user_signup_twitter(var_nick, var_email, var_username, var_password1, var_password2, var_newsletter, var_twitterid) {
	method_json('/includes/functions_methods.php?mtid=202&nick='+var_nick+'&email='+var_email+'&username='+var_username+'&password1='+var_password1+'&password2='+var_password2+'&newsletter='+var_newsletter+'&twitterid='+var_twitterid+'&json=1');
}


function user_password_reset(var_email) {
	$("#jdimmer").fadeOut(500);
	method_json('/includes/functions_methods.php?mtid=203&email='+var_email+'&json=1');
}

function user_newsletter_opt(var_email, var_optout) {
	if(var_optout == false) {
		method_json('/includes/functions_methods.php?mtid=110&email='+var_email+'&json=1');
	} else {
		method_json('/includes/functions_methods.php?mtid=111&email='+var_email+'&json=1');
	}
}

function user_newsletter_opt_noreciept(var_email, var_optout) {
	if(var_optout == false) {
		method_json('/includes/functions_methods.php?mtid=110&email='+var_email+'&reciept=0&json=1');
	} else {
		method_json('/includes/functions_methods.php?mtid=111&email='+var_email+'&reciept=0&json=1');
	}
}

function user_profile_save(var_form, var_silent) {
	var var_url = '';
	var var_element = document.getElementById(var_form).elements;
	
	if(var_silent == '') {
		var_silent = 0;	
	}
	
	for(var i = 0; i < var_element.length; i++)
	{
		var_url += '&'+var_element[i].name+'='+url_encode(var_element[i].value);
	} 
	
	method_json('/includes/functions_methods.php?mtid=205'+var_url+'&silent='+var_silent+'&json=1');
}

function user_remove(var_osid) {
	method_json('/includes/functions_methods.php?mtid=211&osid='+var_osid+'&json=1');
}

function user_settings_save(var_params) {
	method_json('/includes/functions_methods.php?mtid=207&'+var_params+'&json=1');
}

function user_model_add(var_params) {
	method_json('/includes/functions_methods.php?mtid=204&'+var_params+'&json=1');
}

function user_model_profile_save(var_params) {
	method_json('/includes/functions_methods.php?mtid=209&'+var_params+'&json=1');
}

function user_like_add(var_uid) {
	method_json('/includes/functions_methods.php?mtid=220&uid='+var_uid+'&addremove=1&json=1');
}

function user_like_remove(var_uid) {
	method_json('/includes/functions_methods.php?mtid=220&uid='+var_uid+'&addremove=0&json=1');
}

function user_bookmark_add(var_uid) {
	method_json('/includes/functions_methods.php?mtid=221&uid='+var_uid+'&addremove=1&json=1');
}

function user_bookmark_remove(var_uid) {
	method_json('/includes/functions_methods.php?mtid=221&uid='+var_uid+'&addremove=0&json=1');
}

function user_alert_add(var_pid, var_text) {
	method_json('/includes/functions_methods.php?mtid=222&pid='+var_pid+'&text='+var_text+'&addremove=1&json=1');
}

function user_alert_remove(var_pid, var_text) {
	method_json('/includes/functions_methods.php?mtid=222&pid='+var_pid+'&text='+var_text+'&addremove=0&json=1');
}

function user_favorite_add(var_fid) {
	method_json('/includes/functions_methods.php?mtid=223&fid='+var_fid+'&addremove=1&json=1');
}

function user_favorite_remove(var_fid) {
	method_json('/includes/functions_methods.php?mtid=223&fid='+var_fid+'&addremove=0&json=1');
}

function user_favorite_publisher_add(var_pid) {
	method_json('/includes/functions_methods.php?mtid=224&pid='+var_pid+'&addremove=1&json=1');
}

function user_favorite_publisher_remove(var_pid) {
	method_json('/includes/functions_methods.php?mtid=224&pid='+var_pid+'&addremove=0&json=1');
}

function user_category_add(var_name, var_list) {
	var_list = var_list.replace('+', '%2B');
	method_json('/includes/functions_methods.php?mtid=208&name='+var_name+'&category_list='+var_list+'&addremove=1&json=1');
}

function user_category_remove(var_cgid) {
	method_json('/includes/functions_methods.php?mtid=208&cgid='+var_cgid+'&addremove=0&json=1');
}

function user_image_save(var_imgid, var_type, var_caption) {
	method_json('/includes/functions_methods.php?mtid=212&image_id='+var_imgid+'&image_type='+var_type+'&image_caption='+url_encode(var_caption)+'&json=1');
}

function user_image_remove(var_imgid) {
	method_json('/includes/functions_methods.php?mtid=212&image_id='+var_imgid+'&image_type=-1&image_caption=-1&json=1');
}

function user_message_send(var_to, var_message, var_reply) {
	method_json('/includes/functions_methods.php?mtid=210&to='+var_to+'&message='+var_message+'&reply='+var_reply+'&type=1&folder=1&json=1');	
}

function user_message_read(var_umid) {
	method_json('/includes/functions_methods.php?mtid=210&umid='+var_umid+'&folder=-1&json=1');
}

function user_message_unread(var_umid) {
	method_json('/includes/functions_methods.php?mtid=210&umid='+var_umid+'&folder=-2&json=1');
}

function user_message_save(var_umid) {
	method_json('/includes/functions_methods.php?mtid=210&umid='+var_umid+'&folder=2&json=1');
}

function user_message_delete(var_umid) {
	method_json('/includes/functions_methods.php?mtid=210&umid='+var_umid+'&folder=3&json=1');
}

// UPDATE FUNCTIONS

function update_comment(uid_name, comment_name, timeline_name, var_keycheck) {
	var var_send = false;
	var var_keyreturn = true;

	var var_uid = document.getElementById(uid_name).value;
	var var_comment = document.getElementById(comment_name).value;
	var var_datastring = 'uid='+ var_uid + '&comment=' + var_comment;
	
	if(var_keycheck == true) {
		if(window.event && window.event.keyCode == 13) {
			var_send = true;
			var_keyreturn = false;
		}
	} else {
		var_send = true;	
	}
	
	if(var_comment != '' && var_send == true ) {
		$.ajax({
			type: "POST",
			url: "/includes/content_comment.php",
			data: var_datastring,
			cache: false,
			success: function(html){
				$("ol#"+timeline_name).append(html);
				$("ol#"+timeline_name+" li:last").fadeIn("slow");
				document.getElementById(uid_name).value='';
				document.getElementById(comment_name).value='';
				$("#"+comment_name).focus();
			}
		});
	}
	
	return var_keyreturn;
}
