$(document).ready( function(){
	$("body").addClass("js-active");

	new FullToggleToolbar(); /* ADDED 23-1-2010 */

	$("#toggle-frame").click(function(e) {
		if(parent.toggleFrame) {
			parent.toggleFrame();
			$("body").toggleClass("minimized");
			e.preventDefault();
		}
	});

	//new Rating(); /* CHANGED 23-1-2010 */
	//new Tabs(); /* CHANGED 23-1-2010 */
	new CustomSubmits();

	/* CHANGED 23-1-2010 */
	/*
	$(window).bind('load', function(){
		$('body').toggleClass('tickle');
	});
	*/
});

FullToggleToolbar = function() {
    // mechanism interaction
	$("#full_toggle_toolbar").hover(function() {
        // mouseOver
		   document.getElementById('full_toggle_toolbar_button').style.backgroundPosition = '0 -14px';
	},
	function() {
        // initialize
           var body_classname = document.getElementsByTagName('body')[0].className;

		// mouseOut
		   if(body_classname == "js-active minimized"){
	          document.getElementById('full_toggle_toolbar_button').style.backgroundPosition = '0 0';
	       }
	}).click(function(e) {
        // onClick
	       document.getElementById('full_toggle_toolbar_button').style.backgroundPosition = '0 0';

        // changeToolbar
		   if(parent.toggleFrame) {
			  parent.toggleFrame();
			  $("body").toggleClass("minimized");
			  e.preventDefault();
		   }
	});
}

Rating = function() {
	// initialise (apply average)
	var av = $("#ratings-average strong");
	var submitted = false;

	if(av) {
		var num = parseFloat(av.text());
		for(var i = 0; i < Math.floor(num); i++) $("#ratings-form label").eq(i).addClass("full");
		if(num > Math.floor(num)) $("#ratings-form label").eq(Math.floor(num)).addClass("half");
	}

	// mechanism interaction
	$("#ratings-form label").hover(function() {
		// rollover
		$(this).addClass("hover").prevAll().addClass("hover");
		$(this).nextAll().removeClass("hover");
	},
	function() {
		// rollout
		$(this).removeClass("hover").siblings().removeClass("hover");
	}).click(function() {
		// click or change
		$(this).addClass("activated").prevAll().addClass("activated");
		$(this).nextAll().removeClass("activated");
		$("input", $(this)).attr({ checked: "checked" });

		//IE submitted the form twice, this check prohibits this
		if(!submitted)
		{
			$("#ratings-form").submit();
			submitted = true;
		}
	});
}

Tabs = function() {
	var self = this;
	this.set = function(target) {
		var source = target? target.parents('ul').find('a') : $("ul.tabs a");
		source.each(function() {
			var link = $(this),
				item = link.parent(),
				list = item.parent();

			var current = target || $("li.current a", list);

			var h = link.attr("href");
			if(current && current.attr("href") == h) {
				$(h).show();
				item.addClass("current");
			} else {
				$(h).hide();
				item.removeClass("current");
			}
		});

		$('body').toggleClass('tickle');
	}

	this.set();

	$("ul.tabs a").click(function(e) {
		self.set($(this));
		var html = $(this).html();
		var type = /reactie/i.test(html)? 'comments' : 'ratings';

		if(parent.toggleFrame) {
			if(/(plaats|geef)/i.test(html)) {
				parent.toggleFrame('large', type);
			} else {
				parent.toggleFrame('medium', type);
			}
		}
		e.preventDefault();
	});
}

CustomSubmits = function() {
	$("input.submit.custom").each(function() {
		/* CHANGED 30-12-2009 */
		var a = jQuery('<div class="submit_button"><a href="#">' + $(this).attr("value") + '</a></div>');
		// var a = jQuery('<a href="#" class="button"><span><span><span>' + $(this).attr("value") + '</span></span></span></a>');
		$(this).after(a);
		$(this).hide();
		a.click(function(e) {
			$(this).parents("form").submit();
			e.preventDefault();
		});
	});

	/* VALIDATION SUBMITBUTTON - ADDED 4-3-2010 */
	$("input.submit.customValidation").each(function() {
		/* ADDED 4-3-2010 */
		var a = jQuery('<div class="submit_button"><a href="#">' + $(this).attr("value") + '</a></div>');
		$(this).after(a);
		$(this).hide();
		a.click(function(e) {

		self.focus();

		    // Initialize the url(s)
		       var objUrlChecked = document.getElementById('frame_validation_url').value;
		       var objUrl = document.getElementById('url').value;

		    // Replace first part of the url when its declarated
               objUrl = objUrl.replace("http://", "");

		    // Check if there is a match with "www."
               var objUrlMatch = objUrl.match("www.");

  		    // Check if the url is (still) a valid url
		       var urlValidation_1 = objUrl.match(/^[a-zA-Z0-9\.-]+\.[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$/);

  		    // When there is no match add "www."
  		       if(objUrlMatch == null && urlValidation_1 == null){ objUrl = 'www.'+objUrl; }

  		    // Check if the url is (still) a valid url
		       var urlValidation_2 = objUrl.match(/^www+\.[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$/);

  		    // Check if the url is (still) a valid url
		       var urlValidation_3 = objUrl.match(/^[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}\/(.*)$/);

  		    // Check if the url still passes the rules for the frame validation
		       if(!(objUrlChecked == objUrl) && (!(urlValidation_1 == null) || !(urlValidation_2 == null) || !(urlValidation_3 == null)) && !(objUrl == "")){
                    // Start frame validation
		               frame_validation_window(objUrl, 'validation', 'none');
               }

  		    // Submit the form
			   $(this).parents("form").submit();
			   e.preventDefault();
		});
	});
	/* VALIDATION SUBMITBUTTON - ADDED 4-3-2010 */
}

// Control the browser width for the file "frame_vote.php"
   function checkbrowserwidth(old_width){
      // Check if the variabel with the previous width is different then the width now
         if(!(old_width == document.body.clientWidth)){
            // Check if the banner should be changed
               if(document.body.clientWidth >= "1200"){
               	  // Put the large banner on the screen
                     document.getElementById('banner_small').style.display = 'none';
                     document.getElementById('banner_large').style.display = 'block';
               } else {
               	  // Put the small banner on the screen
                     document.getElementById('banner_small').style.display = 'block';
                     document.getElementById('banner_large').style.display = 'none';
               }
         }

      // Set the previous width
         old_width = document.body.clientWidth;

      // Reload the function every 200 milliseconds
         setTimeout("checkbrowserwidth()", 200);
   }

// Load the file that will initialize the new url for the page "view_website.php"
   function attach_framevote_new_url(id, url, cat, com, type){
      // Add one item to the url statistics
         OneStat_Js_Pageview(url, 'url');

      // Remove previous opened framevote new url JS-files from head
         // Declarate variables
            var allsuspects = document.getElementsByTagName('script');
            var filename = "static/js/framevote_new_url.php";

         // Start the tracing of the JS-files
            for(var i=0; i<=allsuspects.length; i++){
                // Check if the JS-file is loading the filename
                   if(allsuspects[i] && allsuspects[i].getAttribute('src')!=null && allsuspects[i].getAttribute('src').indexOf(filename)!=-1){
                      // Remove the JS-files
                         allsuspects[i].parentNode.removeChild(allsuspects[i]);
                   }
            }

      // Create new script element, set its relative URL, and load it
         script = document.createElement( 'script' );
         script.src = 'static/js/framevote_new_url.php?id='+id+'&cat='+cat+'&com='+com+'&type='+type+'&url='+url;
         document.getElementsByTagName( 'head' )[0].appendChild( script );
   }

// Set zoekplezier.nl as browser homepage
   var browser_homepage_click     = 'n';
   var browser_homepage_mouseover = 'n';
   var browser_homepage_mouseout = 'n';

   function browser_homepage(action){
      if(document.all && action == 'click'){
         browser_homepage_click = 'y';

         var url = "http://www.zoekplezier.nl";
         document.body.style.behavior='url(#default#homepage)';
         document.body.setHomePage(url);

         var setTimeout_action = 'mouseout';
         setTimeout(" browser_homepage_click = 'n'; browser_homepage('"+setTimeout_action+"'); ", 200);
      } else if(!(document.all)){
         // Voeg toe aan favorieten met CTRL D
         document.getElementById('startpage_text').innerHTML = 'Voeg Zoekplezier toe als favoriet met CTRL D';
      }

      if(action == 'mouseover' && browser_homepage_click == 'n' && document.getElementById('startpage_box').offsetWidth == 0){
         browser_homepage_mouseover = 'y';
         $('#startpage_box').animate({width:'toggle'}, 500, 'linear', function() { document.getElementById('startpage_text').style.display = 'block'; browser_homepage_mouseover = 'n'; if(browser_homepage_mouseout == 'y'){ browser_homepage_mouseout = 'n'; setTimeout("browser_homepage('"+"mouseout"+"')", 800); } } );
      }

      if(action == 'mouseout' && browser_homepage_click == 'n' && document.getElementById('startpage_box').offsetWidth == 232){
      	 document.getElementById('startpage_text').style.display = 'none';
      	 $('#startpage_box').animate({width:'toggle'}, 500);
      } else if(action == 'mouseout' && browser_homepage_click == 'n' && browser_homepage_mouseover == 'y'){
         browser_homepage_mouseout = 'y';
      }
   }

// Homepage - Startbutton triggered
   function homepage_startbutton(){
      document.homepage.submit();
   }

// Homepage - Alt triggered
   function homepage_alt(e, text) {
      var IE = document.all?true:false
      //if(!IE) document.captureEvents(Event.MOUSEMOVE)

      var tempX = 0
      var tempY = 0

      obj = document.getElementById('homepage_alt');

      if(obj.style.display == "none" || obj.style.display == ""){
         if(IE){
            tempX = event.clientX + document.body.scrollLeft
            tempY = event.clientY + document.body.scrollTop
         } else {
            tempX = e.pageX
            tempY = e.pageY
         }

         if(tempX < 0){ tempX = 0; }
         if(tempY < 0){ tempY = 0; }

         obj.style.left = tempX+'px';
         obj.style.top = (tempY-20)+'px';
         obj.innerHTML = text;
         obj.style.display='block';

         setTimeout("obj.style.display='none'",2500);
      }
   }

// Homepage - Alt mouseover
   function homepage_mouseover(){
      document.getElementById('startpage_box').style.color = '#cc0000';
   }

// Homepage - Alt mouseout
   function homepage_mouseout(){
      document.getElementById('startpage_box').style.color = '#000000';
   }

// Manipulate the onestat.com code and add one item to the statistics
   function OneStat_Js_Pageview(inbaso_value, inbaso_type){
      var d=document;
      var sid="546697";
      var CONTENTSECTION="";

      //if(!(inbaso_type == 'url')){   alert('ja');
         //var osp_URL= 'http://www.zoekplezier.nl/view_website.php/category/'+inbaso_value+'/';
      //} else {
         var osp_URL= 'http://www.zoekplezier.nl/view_website.php/url/'+inbaso_value;
      //}

      var osp_Title=d.title;
      var t=new Date();
      var p="http"+(d.URL.indexOf('https:')==0?'s':'')+"://stat.onestat.com/stat.aspx?tagver=2&sid="+sid;
          p+="&url="+escape(osp_URL);
          p+="&ti="+escape(osp_Title);
          p+="&section="+escape(CONTENTSECTION);
          p+="&rf="+escape(parent==self?document.referrer:top.document.referrer);
          p+="&tz="+escape(t.getTimezoneOffset());
          p+="&ch="+escape(t.getHours());
          p+="&js=1";
          p+="&ul="+escape(navigator.appName=="Netscape"?navigator.language:navigator.userLanguage);

      //if(osp_URL!=d.URL) p+="&ol="+escape(osp_URL);

      if(typeof(screen)=="object"){
         p+="&sr="+screen.width+"x"+screen.height;p+="&cd="+screen.colorDepth;
         p+="&jo="+(navigator.javaEnabled()?"Yes":"No");
      }

      document.getElementById('ONESTAT_TAG').src = p;
      //d.write('<img id="ONESTAT_TAG" border="0" width="1" height="1" src="'+p+'" >');
   }
