function autoWidth() {
				var myWidth = 0;
				if( typeof( window.innerWidth ) == 'number' ) {
								//Non-IE
								myWidth = window.innerWidth;
				} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
								//IE 6+ in 'standards compliant mode'
								myWidth = document.documentElement.clientWidth;
				} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
								//IE 4 compatible
								myWidth = document.body.clientWidth;
				}

				if(myWidth <= 1152)	{
								document.getElementById('pagewrapper').style.paddingLeft='0px';
				}
				else{
								document.getElementById('pagewrapper').style.paddingLeft='60px';
				}
				
				window.onresize = function() { autoWidth(); };
}

function makeButtons()
{								
				var button = $(".linkbutton, input[type=button], input[type=submit], button");
				button.button();

				/*$( ".disablebutton" ).button( "option", "disabled", true ) <- OMG, zie niks meer in MSIE */
				$( ".disablebutton" ).button( "option", "icons", {primary:'ui-icon-locked',secondary:'ui-icon-locked'} );


				/* Topic buttons */
				$('.buttonsmall').removeClass("ui-button-text-only");
				
				/* IE CSS fix, n.v.t. bij IE9.0 */
				if($.browser.msie && $.browser.version > 6 && $.browser.version < 9 )
				{
								$('.comment-details .buttonsmall').css("top","-10px");
				}
}

function makeZebra()
{
				$("table.rollover tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
}

function checkboxClickable()
{

				$("#messagebox #checkAll").click(function() {
								$("#messagebox input").attr("checked", $('#messagebox #checkAll').is(':checked'));

								$("#messagebox input").each(function(index){
												$(this).parent().parent().toggleClass("selected", $('#messagebox #checkAll').is(':checked'));
								});

//								$("#messagebox input").parent().parent().toggleClass("selected", $('#messagebox #checkAll').is(':checked'));
					});

				$("#messagebox input").click(function() { 

								$(this).parent().parent().toggleClass("selected", $(this).attr("checked"));

								});
}
/*
function hasClass(elt, className)
{
				if(elt.className)
				{
								var classes = elt.className.split(" ");

								for(var i in classes)
								{
												if(classes[i] == className)
																return true;
								}
				}
				return false;
}
*/

function messagebox_link_flash(turnon)
{
			if(turnon)
			{
							$("#messagebox_link").switchClass('newmail-off', 'newmail-on', 1000);
			}
			else{
							$("#messagebox_link").switchClass('newmail-on', 'newmail-off', 1000);
			}

				window.setTimeout('messagebox_link_flash('+ (!turnon) +')', 700);
}

$(function() {


				autoWidth();

				$('.tab-inactive').hover(function() {
								$(this).attr('class', 'tab-active');
				}, function() {
								$(this).attr('class', 'tab-inactive');
				});


				$('#dialog_change_status').dialog({
								//	selected: 2,     // which tab to start on when page loads
								autoOpen: false,
								resizable: false,
								width: 450,
								modal: true,
								buttons: {
												"OK": changestatus_OKbutton,
												"Annuleer": function() {
																$(this).dialog("close");
												}
								}
				});

    $('.ui-dialog-buttonpane button:contains(OK)').addClass("dialog_okbutton");

					/*
					* Searchbox (valueclick)

				var searchBox = $("#search");

				searchBox.focus(function(){
								if($(this).attr("value") == "Zoeken") $(this).attr("value", "");
				});

				searchBox.blur(function(){
								if($(this).attr("value") == "") $(this).attr("value", "Zoeken");
				});
									*/

					/*
					* Universal valueclick
					*/
				var valueclick = $(".valueclick");

				// default value
				valueclick.each(function() {
								$(this).attr("value", $(this).attr("title"));
								$(this).addClass("valueclick");
				});

				valueclick.focus(function(){
								if($(this).attr("value") == $(this).attr("title"))
								{
												$(this).attr("value", "");
												$(this).removeClass("valueclick");
								}
				});

				valueclick.blur(function(){
								if($(this).attr("value") == "")
								{
												$(this).attr("value", $(this).attr("title"));
												$(this).addClass("valueclick");
								}
				});



				makeButtons();

				makeZebra();

				checkboxClickable();

					// Click cell - redirect to link
				$('table.clickcell tbody tr').click(function(){

								var value = $(this).closest('tr').find('td:first').find('a:last').attr('href');
								if(value)	window.location = value;
								return false;

				});
				


// Als deze ID bestaat dan een timer instellen
				var iets = $("#messagebox_link");
				
			 if(iets){
								
								 messagebox_link_flash(true);
				}

				/*
				 * Captcha refresher
				 */
				$("#refresh").click(function() {
								var src = $("#captcha").attr("src");
								var time=new Date().getTime();

								$("#captcha").attr("src", "/captcha" + "//" + time);
				});


					$("#more_emoticons").click( function() {
									
								$.get('/ajax/forum-emoticons', { }, function(output){
												$("#more_emoticons").after(output);
												$("#more_emoticons").hide();
								}, "text");

								return false;
					}	);


					$(".opendialog_change_status").click( function() {


								/**
									* Autocomplete location
									*/
								var cache = {},
								lastXhr;
								$( "#status_location" ).autocomplete({
												minLength: 1,
												source: function( request, response ) {
																var term = request.term;
																if ( term in cache ) {
																				response( cache[ term ] );
																				return;
																}

																lastXhr = $.getJSON( "/ajax/oja-searchlocation/", request, function( data, status, xhr ) {
																				cache[ term ] = data;
																				if ( xhr === lastXhr ) {
																								response( data );
																				}
																});
												}
								});


							 $("#dialog_change_status").dialog("open");

								return false;
					}	);

});

var changestatus_OKbutton = function(){

								$("#status_message, #status_location, #updatestatus").attr("disabled", "disabled");

								var status						= $("#status_message").val();
								var location				= $("#status_location").val();
								var sid									= $("#sid").val();

								if($("#current_status")) $("#current_status").fadeOut("slow");

								$.post("/ajax/oja-updatestatus", {status: status, location: location, sid: sid}, function (result) {

            /* disable loading*/
            $(".dialog_okbutton").attr("disabled", "");
            $(".dialog_okbutton").html('<span class="ui-button-text">OK</span>');

												if(!result.error)
												{
																$("#status_message, #status_location").val('');
                
                $("#dialog_change_status").dialog("close");

																if($("#current_status")) $("#current_status").html(result.output);

																// Adition
//																$("#profiletabs").tabs( 'load' , 0 );


												}else {

																$("#dialog_change_status").dialog("open");
																
																alert(result.error);
												}

												if($("#current_status"))		$("#current_status").fadeIn("slow");

												$("#status_message, #status_location, #updatestatus").attr("disabled", "");

								}, "json");



								/*$("#dialog_change_status").dialog("close");*/

        /* enable loading*/
								$(".dialog_okbutton").attr("disabled", "disabled");
        $(".dialog_okbutton").html('<span class="ui-button-text"><img src="/images/icons/ajax-loader.gif" /></span>');

};


$(document).ready(function(){

				/* Firefox adfix */
//				if($.browser.mozilla)	{
//								$("#column_right").css("top", "60px");
//				}
//				$("body").before("docready");

				/* Adfix
				if($.browser.msie && $.browser.version >= 9)	{
								$("#column_right").css("float", "none");
								$("#column_right").css("display", "inline");
				}
				else if($.browser.mozilla && $.browser.version >= 2)	{
								$("#column_right").css("float", "none");
								$("#column_right").css("display", "inline");
								
								$("#column_right").css("top", "60px");
				} */
    
				/*
					* Externe URL's in nieuw venster
					*/
					$("a[rel*=external]").each(function(i){
								this.target="_blank";
				});

				/*
				 * Header tabs accordion
				 */
    lastBlock = $("#menutab-opened");
    maxWidth = 200;
    minWidth = 33;
				eerste_geweest = false;

    $("#menutabs li div").mouseenter(
      function(){
								if($(this).attr('id') != "menutab-opened")
								{
												// Vorige kleiner maken en uitzetten
												$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:300 });

												$(lastBlock).children('img').attr('src', '/images/menutab-' +	$(lastBlock).children('img').attr('alt') + '-off.png');
								}


								// Huidige groter maken en aanzetten
								$(this).animate({width: maxWidth+"px"}, { queue:false, duration:300});
								$(this).children('img').attr('src', '/images/menutab-' +	$(this).children('img').attr('alt') + '-on.png');


									$(lastBlock).attr('id', '');
								lastBlock = this;
      }
    );

				/*
				 *  Plaatjes verkleinen welke te groot zijn
				 */
				$("img.bbimage").each(function() {

								var resized = false;
								var image_only = false;

								// Colorbox maken als geen link voor staat
								if($(this).parent().get(0).tagName != "A")	{
												$(this).colorbox({href: $(this).attr("src")});
												$(this).addClass("pointer");
												image_only = true;
								}

								if($(this).width() > 400)	{
												$(this).attr("width", 400);
												resized = true;
								}
								else if($(this).height() > 400)	{
												$(this).attr("height", 400);
												resized = true;
								}

								if(resized && image_only)	{ //
												$(this).attr("title", $(this).attr("src"));
												$(this).wrap("<div class='bbimage_resized'>Klik op de afbeelding om deze op ware grootte te bekijken.</div>"); // .css({ width: "400px" })
								}
				});
});

function deleteItem(url, id, sid, hideId)
{
				if(!confirm('Weet je zeker dat je dit item wilt verwijderen?')){
								return false;
				}

				$.post(url, {id: id, sid: sid}, function(output){
							
								if(output == "OK")
								{
												$("#"+hideId).hide("explode", {}, 500); 
								}
								else
								{
												alert(output);
								}

				}, "text");

				return false;
}

function copyLink(path)
{
				WndCreatePrompt = prompt("Gebruik de toetsencombinatie [CTRL]+[C] om een directe link van het bericht naar het klembord te kopieeren:", "http://www.mastaline.com/" + path );
				return false;
}

function pollVote( sid, message_id, poll_index, choice )
{
				var poll_id = "#poll_"+message_id+"_"+poll_index;
				var poll = $(poll_id);

				$(poll_id + " tr td").each( (function() {$(this).hide(500);}) );
				poll.append("<tr><td colspan='4' id='polltext_"+ message_id+"_"+poll_index +"'><img src='/images/icons/ajax-loader.gif' /> Bezig met laden..</td></tr>").show(500);

				$.post("/ajax/forum-pollvote", {
								sid: sid,
								message_id: message_id,
								poll_index: poll_index,
								choice: choice
				},
								(function(output){
												poll.html(output);
//																				if(output == "OK")
//																				{
//																								poll.load(document.location.href + " " + poll_id).show(3000);
//																				}
//																				else
//																				{
//																								$("#polltext_"+ message_id+"_"+poll_index).html(output);
//																				}
								}), "text");
				
}

function resetPoll(sid, message_id, poll_index)
{
				var poll_id = "#poll_"+message_id+"_"+poll_index;
				var poll = $(poll_id);
				
				if(confirm('Weet je zeker dat je de poll wilt resetten?'))
				{
								$.post('/ajax/forum-pollreset', {
												sid: sid,
												message_id: message_id,
												poll_index: poll_index
								}, function(output) {
												poll.html(output);
								}, "text");
				}
				return false;
}

function favoriteTopic(sid, topic_id, reload)
{
				$.post('/ajax/forum-favoritetopic', {
								action: "add",
								sid: sid,
								topic_id: topic_id
				}, function(output) {

								if(output == "OK")
								{
												alert("Topic is toegevoegd aan favoriete topics.");
								}
								else if(output == "EXISTS")
								{
												if(confirm('Wil je deze topic verwijderen uit favoriete topics?'))
												{
																$.post('/ajax/forum-favoritetopic', {
																				action: "delete",
																				sid: sid,
																				topic_id: topic_id
																}, function(output) {
																				if(output == "OK")
																				{
																								alert("Topic is verwijderd uit favoriete topics.");

																								if(reload)		{
																												location.reload(true);
																								}
																				}
																});

												}
								}
								else	{
												// Error
												alert(output);
								}
				}, "text");

				return false;
}

function bb(sUBB, eUBB){

				
//				var objTxtArea = document.forms[formName].elements["bericht"];

					 var formName = $('#context_content:first form').attr("name");
						var objTxtArea_name = $('#context_content:first form textarea:first').attr("name");
						var objTxtArea = document.forms[formName].elements[objTxtArea_name];


				if (!eUBB){
								eUBB = ' '+sUBB+' '; // Smileys..
								sUBB = '';
				}
				if (objTxtArea.selectionStart || objTxtArea.selectionStart == 0){
								var ss = objTxtArea.selectionStart;
								var se = objTxtArea.selectionEnd;
								objTxtArea.value = objTxtArea.value.substring(0, ss) + sUBB + objTxtArea.value.substring(ss, se) + eUBB + objTxtArea.value.substring(se, objTxtArea.value.length);
								if (sUBB.length != 0){
												objTxtArea.selectionStart = ss;
												objTxtArea.selectionEnd = se + sUBB.length + eUBB.length;
								}else{
												objTxtArea.selectionStart = se + sUBB.length + eUBB.length;
												objTxtArea.selectionEnd = objTxtArea.selectionStart;
								}
								objTxtArea.focus();
				}else if (objTxtArea.createTextRange){
								objTxtArea.focus();	//Needs to get focus to prevent other objects to get ubb codes
								document.selection.createRange().text = sUBB + document.selection.createRange().text + eUBB;
				}else{
								txtArea.value = txtArea.value + sUBB + eUBB;
				}
}

function PopUp(URL,BREEDTE,HOOGTE)
{
				var wnd=null;
				var random_num = (Math.round((Math.random()*999)));

				wnd = window.open(URL, random_num, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width='+BREEDTE+',height='+HOOGTE+'');

				return false;
}

function isMaxLength(obj)
{
				var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
				if (obj.getAttribute && obj.value.length>mlength)
				obj.value=obj.value.substring(0,mlength)
}
