var loading	=	false;

function alertSize() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth ) ) {
    myWidth = document.body.clientWidth;
  }
  if (myWidth < 1000) {
//		document.getElementById("alert").innerHTML = "TOMMMMMMMMMMMAS";
//		alert("Den passar bättre att ha 1000 pixel eller större");
  }
}

//alertSize();


//alert(this.clientWidth);
function autoLoad(id) {
	$(document).ready(function(){
		commentReader(id);
	});
}

function active(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() { oldonload(); func(); }
	}
}

function eFocus(field){	if (field.value == field.defaultValue){	field.value =''; }}
function eBlur(field){	if (field.value == ''){	field.value = field.defaultValue; }}

function ajaxReader(where,page) {
	var optionsView	=	document.getElementById('writerView').value+
						document.getElementById('orderView').value+
						document.getElementById('tagsView').value;
	var searchTxt	=	document.getElementById('searchTxt').value;	
	var string = '?options='+optionsView+'&searchTxt='+encodeURIComponent(searchTxt)+'&page='+page;
	document.getElementById('oveaLoading').style.display  = ""; 
	$.ajax({
		type: "POST",
		url: "/includes/"+where+".inc.php"+string,
		success: function(msg){
			$("#"+where).html(msg)
			document.getElementById('oveaLoading').style.display  = "none"; 
		}
	});
}

function commentReader(id,page) {
	var string = '?id='+id+'&page='+page;
	$.ajax({
		type: "GET",
		url: "/includes/comments.inc.php"+string,
		success: function(msg){
			$("#comment"+id).html(msg)
		}
	});
}

function formValidator(id,num){
	var str = $("#form"+id).serialize();
	var img = document.getElementById('imgCaptcha'+id); 
	$.ajax({
	type: "POST",
	url: "/comment/writer/",
	data: str,
	success: function(msg){
			if(msg == 'OK') {
				result	=	'<div class="success">Ditt meddelande är lagt in</a>';
				img.src = 'http://ovea.se/images/captcha'+num+'.png?' + Math.random();
				$('#form'+id).each(function(){
					this.reset();
				});				
				$("#success"+id).html(result)
				$("#success"+id).slideDown('slow');
				window.setTimeout(function(){$("#success"+id).slideUp(3000)},5000); 
				commentReader(id,1);
			} else {
				img.src = 'http://ovea.se/images/captcha'+num+'.png?' + Math.random();
				result	=	msg;
				$.prompt(result);
			}
		}
	});
}

function writer(id){
	$("#writer"+id).slideToggle(400)
}

function changeSetup(numeral,point){
	if(this.loading==true) {
		alert("Du måste vänta tills den e klart först");
		return false;
	}	
	var site	= 	document.getElementById('siteView').value;
	if (point=='writer') {
		var writerName	=	new Array();
		writerName[0]="Visa alla"; writerName[1]="Jenny"; writerName[2]="Tomas"; writerName[3]="Guide"; writerName[4]="Länkar"; writerName[5]="Källakod"; writerName[6]="Bilder"; writerName[7]="Video";
		document.getElementById('writerView').value = numeral;
		document.getElementById('authorName').innerHTML = writerName[numeral];
	} else if (point=='order') {
		var orderName	=	new Array();
		orderName[0]="Nyaste"; orderName[1]="Rubrik";
		document.getElementById('orderView').value = numeral;
		document.getElementById('sortName').innerHTML = orderName[numeral];;			
	} else {
		alert(null);
	}
	ajaxReader(site,0);
}

function facebook(url) {
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(url)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function didYouMean(str){
	var site	= 	document.getElementById('siteView').value;
	document.getElementById('searchTxt').value=str;
	ajaxReader(1,site,0);
}

var timeout	= 1000;
var closetimer	= 0;
var ddmenuitem	= 0;

function mopen(id)
{	
	mcancelclosetime();
	if(ddmenuitem) {
		ddmenuitem.style.display = "none";
	}
	ddmenuitem = document.getElementById(id);
	if ($("div#"+id).is(":hidden")) {
		$("div#"+id).slideDown("fast");	
	}
//	$("#drop_author").slideDown('slow');
//	document.getElementById('#drop_author').style.display  = "";
}
function mclose()
{	
	if(ddmenuitem) {
		ddmenuitem.style.display  = "none";
	}
}

function mclosetime()
{	
	closetimer = window.setTimeout(mclose, timeout); 
}
function mcancelclosetime()
{	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}
document.onclick = mclose; 