jQuery.noConflict();

function facebookShare()
{	
	var url;
	if (arguments.length > 0)
	{
		url 	= arguments[0];
	}
	else
	{
		url 	= window.location;
	}

	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(url),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

jQuery(document).ready(function(){
	jQuery(document).pngFix();
	if (jQuery.browser.safari)
	{
		jQuery('body').addClass('safari');
	}
	//prepareCanvas();
	findAndActivateTabArea();
	//prepareFileupload();
	prepareButtonHover();
	
	/*EDIT DEBATE*/
	jQuery('.entry .info .edit a.delete').click(function ()
	{
		edit = jQuery(this).parent();
		edit.hide();
		entry = edit.parent().parent();
		entry.find('.entry-delete').show();
		return false;
	});
	jQuery('.entry .info .edit a.edit-entry').click(function ()
	{
		edit = jQuery(this).parent();
		edit.hide();
		entry = edit.parent().parent();
		entry.find('.entry-change-text').show();
		entry.find('.entry-content').hide();
		return false;
	});
	jQuery('.entry .entry-delete .confirm .t a').click(function ()
	{
		edit = jQuery(this).parent().parent().parent().parent();
		edit.hide();
		entry = edit.parent();
		entry.find('.info .edit').show();
		return false;
	});
	jQuery('.entry .entry-change-text .confirm .t a').click(function ()
	{
		edit = jQuery(this).parent().parent().parent().parent();
		edit.hide();
		entry = edit.parent();
		entry.find('.info .edit').show();
		entry.find('.entry-content').show();
		return false;
	});
	
	jQuery('.entry .entry-change-text .confirm .s img').click(function ()
	{
		form = jQuery(this).parent().parent().parent();
		form.submit();
		return false;
	});
	jQuery('.entry .entry-delete .confirm .s img').click(function ()
	{
		form = jQuery(this).parent().parent().parent();
		form.submit();
		return false;
	});
});

var btns;
function prepareButtonHover()
{
	btns = {
		send: new Image(),
		sendHover: new Image(),
		save: new Image(),
		saveHover: new Image(),
		createtip: new Image(),
		createtipHover: new Image(),
		createprofile: new Image(),
		createprofileHover: new Image(),
		choosefile: new Image(),
		choosefileHover: new Image(),
		addmorelinks: new Image(),
		addmorelinksHover: new Image()
	};
	
	btns.send.src 				= global_url + 'image/btn.send.gif';
	btns.sendHover.src 			= global_url + 'image/btn.send.hover.gif';
	btns.save.src 				= global_url + 'image/btn.save.gif';
	btns.saveHover.src 			= global_url + 'image/btn.save.hover.gif';
	btns.createtip.src 			= global_url + 'image/btn.createtip.gif';
	btns.createtipHover.src 	= global_url + 'image/btn.createtip.hover.gif';
	btns.createprofile.src 		= global_url + 'image/btn.createprofile.gif';
	btns.createprofileHover.src = global_url + 'image/btn.createprofile.hover.gif';
	btns.choosefile.src 		= global_url + 'image/btn.choosefile.gif';
	btns.choosefileHover.src 	= global_url + 'image/btn.choosefile.hover.gif';
	btns.addmorelinks.src 		= global_url + 'image/btn.addmorelinks.gif';
	btns.addmorelinksHover.src 	= global_url + 'image/btn.addmorelinks.hover.gif';
}

function prepareCanvas()
{
	/*
	var height;
	if (document.documentElement && document.documentElement.clientHeight)
	{
		height = document.documentElement.clientHeight;
	}
	else if (document.body.clientHeight)
	{
		height = document.body.clientHeight;
	}
	*/
}

function findAndActivateTabArea()
{
	var i = 0;
	jQuery('.tabarea .tab').slice(0, 1).addClass('selected');
	jQuery('.tabarea .contentarea')
		.each(function(){
			jQuery(this).find('.subcontent').slice(0, 1).addClass('selected');
		})
		.slice(0, 1).addClass('selected');
	
	jQuery('.tabarea .tab').click(function(){ 
		var id = /([0-9]+)/.exec(this.id)[1];
		jQuery('.tabarea .tab.selected, .tabarea .contentarea.selected').removeClass('selected');
		jQuery('#tab_' + id + ', #contentarea_'+id).addClass('selected');
	});
}

function showSubcontent(content_id, subcontent_id)
{
	jQuery('#contentarea_' + content_id + ' .subcontent')
		.removeClass('selected');
	jQuery('#contentarea_' + content_id + ' #subcontent_' + content_id + '_' + subcontent_id)
		.addClass('selected');

	jQuery('#contentarea_' + content_id)
		.find('.pagination .active')
		.removeClass('active')
		.next('.page')
		.removeClass('afterActive');

	jQuery('#contentarea_' + content_id)
		.find('.pagination .page')
		.slice(subcontent_id, subcontent_id+1)
		.addClass('active')
		.next('.page')
		.addClass('afterActive');
	
	var length = jQuery('#contentarea_' + content_id + ' .subcontent').length;
	var arrows = jQuery('#contentarea_' + content_id + ' .pagination .left_arrow,#contentarea_' + content_id + ' .pagination .arrow').removeClass('ghosted');

	if (subcontent_id == 1)
		arrows.slice(0, 1).addClass('ghosted');

	if (subcontent_id == length)
		arrows.slice(1, 2).addClass('ghosted');
}

function nextSubcontent(content_id)
{
	var length = jQuery('#contentarea_' + content_id + ' .subcontent').length;
	var active = parseInt(jQuery('#contentarea_' + content_id + ' .subcontent.selected')
		.attr('id')
		.replace(/^.*?([0-9]+)$/, '$1'));

	if (active == length) return;
	showSubcontent(content_id, active+1);
}

function prevSubcontent(content_id)
{
	var length = jQuery('#contentarea_' + content_id + ' .subcontent').length;
	var active = parseInt(jQuery('#contentarea_' + content_id + ' .subcontent.selected')
		.attr('id')
		.replace(/^.*?([0-9]+)$/, '$1'));

	if (active == 1) return;
	showSubcontent(content_id, active-1);
}

function doLogin()
{
	jQuery('#loginBox form').submit();
}

function showLoginBox()
{
	var $loginBox = jQuery('#loginBox');
	$loginBox.find('.wrongLogin').css('display', 'none');
	$loginBox.css('display', 'block');
	$loginBox.find('.username input')
		.focus(function(){
			$loginBox.find('.forgotPassword').hide();
			$loginBox.find('.rememberLogin').show();
			
			if (jQuery(this).val() == 'Brugernavn')
				jQuery(this).val('');

			$loginBox.find('.username input').blur(function(){
				if (!jQuery(this).val())
				{
					$loginBox.find('.forgotPassword').show();
					$loginBox.find('.rememberLogin').hide();
					jQuery(this).val('Brugernavn');
				}
			});
		})
		.keyup(function(event){
			if (event.keyCode == 13)
				doLogin();
		});
	$loginBox.find('.passwordDummy input')
		.focus(function(){
			$loginBox.find('.forgotPassword').hide();
			$loginBox.find('.rememberLogin').show();
			
			$loginBox.find('.passwordDummy').css('display', 'none');
			$loginBox.find('.password').css('display', 'block');
			$loginBox.find('.password input')
				.focus()
				.blur(function(){
					if (!jQuery(this).val())
					{
						$loginBox.find('.forgotPassword').show();
						$loginBox.find('.rememberLogin').hide();
			
						$loginBox.find('.passwordDummy').css('display', 'block');
						$loginBox.find('.password').css('display', 'none');
					}
				})
				.keyup(function(event){
					if (event.keyCode == 13)
						doLogin();
				});
		})
	$loginBox.find('.loginLink a')
		.click(function(){
			doLogin();
			return false;
		});
}

function hideLoginBox()
{
	jQuery('#loginBox').css('display', 'none');
}

function doForgotLogin()
{
	jQuery('#forgotLoginBox form').submit();
}

function showForgotLoginBox()
{
	jQuery('#forgotLoginBox').css('display', 'block');
	jQuery('#forgotLoginBox .email input')
		.focus(function(){
			if (jQuery(this).val() == 'E-mail')
				jQuery(this).val('');

			jQuery('#forgotLoginBox .email input').blur(function(){
				if (!jQuery(this).val())
					jQuery(this).val('E-mail');
			});
		})
		.keyup(function(event){
			if (event.keyCode == 13)
				doForgotLogin();
		});
}

function hideForgotLoginBox()
{
	jQuery('#loginBox, #forgotLoginBox').css('display', 'none');
}

function hideForgotLoginConfirmationBox()
{
	jQuery('#loginBox, #forgotLoginConfirmationBox').css('display', 'none');
}

function popup(url, w, h)
{
	w = w || 831;
	h = h || 552;
	
	pos_horizon = ((screen.width/2)-(w/2));
	pos_vertical = ((screen.height/2)-(h/2));
	
	return window.open(url, 'popup', 'width=' + w + ',height=' + h + ',left='+pos_horizon+',top='+pos_vertical);
}

function popupImage(gallery_id, image_id)
{
	popup(global_url + 'popup.imageview.php?gallery_id=' + gallery_id + '&image_id=' + image_id, 800, 640);
}

function searchTips(searchtext)
{
	var form = jQuery('form#search_tips_hidden');
	form.find('input[name="searchtext"]').val(searchtext);
	form.submit();
}

function searchProfiles(searchtext)
{
	var form = jQuery('form#search_profiles');
	form.find('input[name="condition"]').val(searchtext);
	form.submit();
}

var tempSrc, mouseTimeout;
var teaser = {
	mouseover: function(parent, id){
		if (window['teaser'+id])
		{
			tempSrc = jQuery(parent).find('img').attr('src');
			jQuery(parent).find('img').attr('src', window['teaser'+id].src);
		}
	},
	mouseout: function(parent, id){
		jQuery(parent).find('img').attr('src', tempSrc);
	}
}

function uniqueId()
{
	var chars = 'abcdefghijklmnopqrstuvwxyz';
	var str = '';
	for (var i = 0; i < 10; i++)
	{
		str = str + chars.substr(Math.floor(Math.random()*chars.length), 1);
	}
	return str;
}

function actionOnLogin(action, info)
{
	var expire = new Date();
	expire.setDate(expire.getDate()+1);
	setcookie('actionOnLogin', action+';'+info, expire, '/');
}

var progressIcon = new Image();
progressIcon.src = global_url + 'image/progress.gif';

function wallSubscription(checkbox, type, wall_id)
{
	var icon = jQuery('<img src="' + progressIcon.src + '" />');
	var subscribe = checkbox.checked;
	var action = subscribe ? 'subscribe' : 'unsubscribe';
	
	var start = new Date().getTime();
	
	jQuery(checkbox)
		.css('display', 'none')
		.parent()
		.prepend(icon);
		
	jQuery.post(
		global_url + 'ajax/wall/'+action+'/',
		{
			type: type,
			wall_id: wall_id
		},
		function(){
			var hide = function(){
				jQuery(checkbox).css('display', 'inline');
				jQuery(icon).remove();
			}
			var end = new Date().getTime();
			var diff = end-start;
			if (diff < 500)
			{
				setTimeout(hide, 500-diff);
			}
			else
			{
				hide();
			}
		}
	);
}

/*
 * Cookie functions
 */
 
function setcookie(name, value, expires, path, domain, secure) 
{
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // *     example 1: setcookie('author_name', 'Kevin van Zonneveld');
    // *     returns 1: true
 
    expires instanceof Date ? expires = expires.toGMTString() : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
    var r = [name + "=" + escape(value)], s, i;
    for(i in s = {expires: expires, path: path, domain: domain}){
        s[i] && r.push(i + "=" + s[i]);
    }
    return secure && r.push("secure"), document.cookie = r.join(";"), true;
}

function getcookie(c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1)
		{ 
			c_start = c_start + c_name.length+1; 
			c_end	= document.cookie.indexOf(";", c_start);

			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		} 
	}
	return "";
}

function wallNavigate(page)
{
	if (page !== false)
	{
		var path = location.pathname.replace(new RegExp('^/', ''), '').split('/');
		path.splice(2, 10);
		location.href = global_url + path.join('/') + '/' + page + '#wall';
	}
}