// JavaScript Document

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//window.onload = MM_preloadImages('images/nav_over.gif','images/nav_last_over.jpg');

function isChrome()
{
	return navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
}

function showhide(targetid,waarde)
{
	var target = document.getElementById(targetid);
	if (waarde == 1) {
		target.style.display = "block";
	} else {
		target.style.display = "none";
	}
}

function addToFav() {
	 title = document.title; 
	 url = window.location;
	
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title); }
		else if(window.opera && window.print) { // Opera Hotlist
			return true; }
}

function toggleVisibilty(divid)
{
	if (document.getElementById(divid).style.display=='block') {
		$("#" + divid).fadeOut(250);
	} else {
		$('#' + divid).fadeIn(250);
	}
}

function toggleVisibiltyFromSubSet(divid,elem)
{
	if (document.getElementById(divid)) {
		if (document.getElementById(divid).style.display == 'block') {
			$("#" + divid).fadeOut(250);
		}
		else {
			$('#' + divid).fadeIn(250);
		}
	}
	if (document.getElementById(elem)) {
		document.getElementById(elem).style.display = 'none';
	}
}

function SetckContents(elem)
{
	if (document.getElementById(elem) && !isChrome()) {
		// Get the editor instance that we want to interact with.
		var my_id = elem; //this is the same as the id of the replaced text field
		var the_ed = CKEDITOR.instances[my_id]
		if (the_ed) {
			var data = the_ed.getData();
			
			document.getElementById(elem).value = data;
		}
	}	 
}

$(document).ready(function() {
	if (document.getElementById('country')){

	$('select#country').val($('#countryid').val());
	$('select#country').selectmenu({
		icons: [
			{find: '.nl'},
			{find: '.fr'},
			{find: '.be'},
			{find: '.es'},
			{find: '.it'},
			{find: '.lu'},
			{find: '.ch'},
			{find: '.at'},
			{find: '.de'},
			{find: '.ie'},
			{find: '.uk'}
		]
	});
	$('select#country').live('change', function(){
		$("select option:selected").each(function () {
	    	document.location.href = $(this).attr('alt');
	 });
	});
	}

	$("ul.verticaal:odd li").css("background-color","#e6e6e6");		 
	$("ul.verticaal:odd").css("background-color","#e6e6e6");		 

	$("div.formulierveldFull ul li:nth-child(even)").css("background-color","#e6e6e6");		 
	$("table.vraag tr td.sm_answer:nth-child(even)").css("background-color","#e6e6e6");		 
	$("table.vraag tr.liners:nth-child(even)").css("background-color","#e6e6e6");		 	
	/*
	var options = { 
        target:        '#loginBoodschap',   // target element(s) to be updated with server response 
        success:       reloadPage  // post-submit callback 
    };  	
    // bind form using 'ajaxForm' 
    $('#loginform').ajaxForm(options); 
	
	var opties = { 
        target:        '#loginboodschap2',   // target element(s) to be updated with server response 
        success:       reloadPage  // post-submit callback 
    };  	
    // bind form using 'ajaxForm' 
    $('#loginformulier').ajaxForm(opties); 
	*/
	var veiligheidsopties = { 
        target:        '#coderesponse',   // target element(s) to be updated with server response 
        success:       threadBlogPost  // post-submit callback 
    };  	
    // bind form using 'ajaxForm' 
    $('#reactieform').ajaxForm(veiligheidsopties); 
	initTabs(); initCampaignTabs(); //initBubbles();
	 
});

function initBubbles(){
	$('.naamreactor').CreateBubblePopup({
		position: 'top',
		align: 'center',
		innerHtml: '<img src="js/bubble/jquerybubblepopup-theme/loading.gif" style="border:0px; vertical-align:middle; margin-right:10px; display:inline;" />loading',
		innerHtmlStyle: { color:'#FFFFFF', 'text-align':'left' },
		themeName: 'all-black',
		themePath: 'js/bubble/jquerybubblepopup-theme'
  	  });
  	  $('.naamreactor').mouseover(function(){
			var button = $(this);
			$.get("index.php?section=ajax/getinsiderdata", { id: button.attr('id')},function(data){
				var seconds_to_wait = 1;
				function pause(){
					var timer = setTimeout(function(){
						seconds_to_wait--;
						if(seconds_to_wait > 0){
							pause();
						}else{
							button.SetBubblePopupInnerHtml(data, false); //false -> it shows new innerHtml but doesn't save it, then the script is forced to load everytime the innerHtml... 
						};
					},1000);
				};pause();
			}); 
		});
}

function strpos( haystack, needle, offset){
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

function reloadPage(status)  {
	return false;
    if (status.substring(0,11) == "<!-- OK -->") {
		url = status.substring(11);
		if (!strpos(document.location, 'country', 0)) {
			curl = document.location + url;
			location.href='http://www.theinsiders.eu/site/index.php?section=home/home' + url;
		}
		else {
			location.reload();
		}
	}
}

function threadBlogPost(status)
{
	if (status == "<!-- OK -->") {
		document.getElementById('reactie').value='';
		document.getElementById('veiligheidscode').value='';
		tekst = document.getElementById('tkst_success').value;
		$("#coderesponse").html(tekst);		
		setTimeout("location.reload();",1500);
	}
}

 
function initTabs() {
	$('#tabboxmenu a').bind('click',function(e) {
		e.preventDefault();
		var thref = $(this).attr("href").replace(/#/, '');
		$('#tabmenu a').removeClass('active');
		$(this).addClass('active');
		$('#boxcontent div.tabcontent').fadeOut(1);
		$('#'+thref).fadeIn(700);
	});
}

function initTab2() {
	$('#tabkey2').bind('click',function(e) {
		e.preventDefault();
		var thref = $(this).attr("href").replace(/#/, '');
		$('#main_camp_nav_blok a').removeClass('active');
		$('a.tbltab_2').addClass('active');
		$('#main_camp_boxcontent div.tabcontent').fadeOut(1);
		$('#tab_2').fadeIn(700);
	});
}

function initTab3() {
	$('#tabkey3').bind('click',function(e) {
		e.preventDefault();
		var thref = $(this).attr("href").replace(/#/, '');
		$('#main_camp_nav_blok a').removeClass('active');
		$('a.tbltab_3').addClass('active');
		$('#main_camp_boxcontent div.tabcontent').fadeOut(1);
		$('#tab_3').fadeIn(700);
	});
}

function initTab4() {
	$('#tabkey4').bind('click',function(e) {
		e.preventDefault();
		var thref = $(this).attr("href").replace(/#/, '');
		$('#main_camp_nav_blok a').removeClass('active');
		$('a.tbltab_4').addClass('active');
		$('#main_camp_boxcontent div.tabcontent').fadeOut(1);
		$('#tab_4').fadeIn(700);
	});
}

function initCampaignTabs() {
	$('#main_camp_nav a').bind('click',function(e) {
		e.preventDefault();
		var thref = $(this).attr("href").replace(/#/, '');
		$('#main_camp_nav a').removeClass('active');
		$(this).addClass('active');
		$('#main_camp_boxcontent div.tabcontent').fadeOut(1);
		$('#'+thref).fadeIn(700);
	});		
}	

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
function toggleReactieTab(id,elem,nieuws_reactietab,urlload,togglePagerContainer,PageContainerValue)
{

	$("#" + nieuws_reactietab + " a").removeClass('active');
	$("#" + elem).addClass('active');
	$("div.reactie_global_box").css({ display:"none" });
	$("div.reactie_global_box").html('');
	$("#box_reacties_" + id).css({ display:"block" });
	if (urlload!=0) {
		$.ajax({
				   type: "GET",
				   url: urlload,
				   data: '',
				   success: function(html){	
				   //	alert(urlload);	
						$("#box_reacties_" + id).html(html);
						initBubbles();
				   }
		 });
		if (urlload.substring(0,33)!="index.php?section=nieuws/reacties") {
			$("div.pager_container").hide(1);
		} else {
			$("div.pager_container").show(1);
		}
	}
	if (togglePagerContainer!=false) {
		if (PageContainerValue == "hide") {
			$("div." + togglePagerContainer).hide(1);
		} else {
			$("div." + togglePagerContainer).show(100);
		}
	}
}   

function updateLikeIt(id,donttext,liketext)
{

	var num = 0;
	if (document.getElementById('like_linkspan_' + id)) {
		num = $("#like_linkspan_" + id).html();
	}
	var ClassName = $('#like_link_' + id).attr('class');
	if (ClassName=="unlikeicon") {
		// al eens geliked, dus unliken
		if (IsNumeric(num) && num>0) {
			num--;
		}		
		$('#like_link_' + id).removeClass('unlikeicon');
		$('#like_link_' + id).addClass('likeicon');				
		$('#like_link_' + id).attr('title',liketext);				
		pushLikeIt(id,'down');
	} else {
		if (IsNumeric(num)) {
			num++;
		}		
		$('#like_link_' + id).removeClass('likeicon');
		setTimeout("$(\'#like_link_" + id + "\').addClass('unlikeicon');",200);				
		$('#like_link_' + id).attr('title',donttext);						
		pushLikeIt(id,'up');		
	}	
	
	$("#like_linkspan_" + id).html(num);				
}

function pushLikeIt(id,direction)
{

	$.ajax({
			   type: "POST",
			   url: "index.php?section=ajax/likeit",
			   data: 'direction=' + direction + '&reactie=' + id,
			   success: function(html){		
			   		//alert('hi');  
					//alert(html); 
					//$("#verloop").html(html);	
			   }
	 });

}

function loadEditors() {
    var $editors = $("textarea.ckeditor");
    if ($editors.length) {
        $editors.each(function() {
            var editorID = $(this).attr("id");
			if (editorID != "reactie") {
				var instance = CKEDITOR.instances[editorID];
				if (instance) {
					CKEDITOR.remove(instance);
				}
				CKEDITOR.replace(editorID);
			}
        });
    }
}


function loadEditor(id)
{
    var instance = CKEDITOR.instances[id];
    if(instance)
    {
        CKEDITOR.remove(instance);
    }
    CKEDITOR.replace(id);
}


function triggerRequestEmail(url)
{
	$.post('index.php?section=ajax/request_activationmail', {
		action: 'request'	
	}, function (data) {
		if (url) {
			window.location.href = url;
		} else {
			$('#popupMessage').html(data);
		}
	});
}

function closePopup()
{
	$('#popupBack').fadeOut(500);
	$('#popupBox').fadeOut(500);	
}
