jQuery(function($) {
	// Framework
	
	(function() {
		if ('undefined' === typeof window.console) {
			window.console = {};
		}
		
		var console_functions = [
			'log', 'debug', 'info', 'warn', 'error', 'assert', 'dir', 'dirxml', 'trace',
			'group', 'groupCollapsed', 'groupEnd', 'time', 'timeEnd', 'profile', 'profileEnd',
			'count', 'exception', 'table'
		];
		
		$
			.each(console_functions, function(k, v) {
				if ('function' !== typeof window.console[v]) {
					window
						.console[v] = $
							.noop;
				}
			});
	})();
	
	$
		.extend(
			true,
			{
				getXML: function(url, data, callback) {
					$.get(url, data, callback, 'xml');
				},
				getHTML: function(url, data, callback) {
					$.get(url, data, callback, 'html');
				},
				getJSONP: function(url, data, callback) {
					$.get(url, data, callback, 'jsonp');
				},
				getText: function(url, data, callback) {
					$.get(url, data, callback, 'text');
				},
				postXML: function(url, data, callback) {
					$.post(url, data, callback, 'xml');
				},
				postHTML: function(url, data, callback) {
					$.post(url, data, callback, 'html');
				},
				postScript: function(url, data, callback) {
					$.post(url, data, callback, 'script');
				},
				postJSON: function(url, data, callback) {
					$.post(url, data, callback, 'json');
				},
				postJSONP: function(url, data, callback) {
					$.post(url, data, callback, 'jsonp');
				},
				postText: function(url, data, callback) {
					$.post(url, data, callback, 'text');
				}
			}
		)
		.ajaxSetup({
			cache: false
		});
	
	$('.target_blank')
		.attr({
			'target': '_blank'
		});

	////chat 
		var textArea = $('textarea#message_send');
		var sendButton = $('input#send_button');
		
		textArea.keypress(function(e)
			{
			code= (e.keyCode ? e.keyCode : e.which);
			if (code == 13){
				var message = textArea.val();
		
				$
					.postJSON(
						'/shout.php',
						{
							'message': message
						},
						function(receivedData) {						
							if (1 === receivedData.status) {
								textArea.val('').focus();
							}
						}
					);
				e.preventDefault();
			}
		});

		
		sendButton.click(function() {
			var message = textArea.val();
			
			$
				.postJSON(
					'/shout.php',
					{
						'message': message
					},						
					function(receivedData) {						
						if (1 === receivedData.status) {
							textArea.val('').focus();
						}
					}
				);
		});
		
	//End chat	
		
	// Vote
	
	$('.box_count div[id^="vote_"]')
		.click(function() {
			var $this = $(this);
			var artId = $this.attr('id').substring(5);
			var addVote = $('#add_vote_' + artId + ' span:first');
			
			$
				.postJSON(
					'/ajax/vote.php',
					{
						'artId': artId
					},
					function(receivedData) {
						var vote = parseInt(addVote.text());
						
						if (1 === receivedData.status) {
							addVote
								.text(vote + 1)
						}
					}
				);
		});
		
	$('.confirm')
		.each(function() {
			var $this = $(this);
			var message = $this.attr('title') || 'Are you sure you want to remove this ad?';
			
			$this
				.click(function(e) {
					if (!confirm(message)) {
						e
							.preventDefault();
					}
				});
		})
		.removeAttr('title');

	//popup VIP
	//0 means disabled; 1 means enabled;
	var popupStatus = 0;

	//disabling popup with jQuery magic!
	function disablePopup(){
		//disables popup only if it is enabled
		if(popupStatus==1){
			$("#popup_background").fadeOut("fast");
			$("#pop").fadeOut("fast");
			popupStatus = 0;
		}
	}


	//CONTROLLING EVENTS IN jQuery
	$(document).ready(function(){
		
		//LOADING POPUP
		//Click the button event!
		$('#show_pop').click(function(){
		//request data for centering
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			var popupHeight = $("#pop").height();
			var popupWidth = $("#pop").width();
			//centering
			$("#pop").css({
				"position": "fixed",
				"top": windowHeight/2-popupHeight/2,
				"left": windowWidth/2-popupWidth/2
			});
			//only need force for IE6
			
			$("#popup_background").css({
				"height": windowHeight
			});
			//load popup
			//loads popup only if it is disabled
			if(popupStatus==0){
				$("#popup_background").css({
					"opacity": "0.7"
				});
				$("#popup_background").fadeIn("fast");
				$("#pop").fadeIn("fast");
				popupStatus = 1;
			}
		});
		
		//CLOSING POPUP
		
		//Click the x event!
		$("#pop_close").click(function(){
			disablePopup();
		});
		
		//Click out event!
		$("#popup_background").click(function(){
			disablePopup();
		});
		
		//Press Escape event!
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		});

	});
	
	// setup ul.tabs to work as tabs for each div directly under div.panes
	$("ul.tabs").tabs("div.panes > div");

});

