var scrolling = 0;

function scrollLeft() {
   if (scrolling == 1) {
      var value = $(".slider").slider("value");
      $(".slider").slider("value", (value - 2));
   }
}

function scrollRight() {
   if (scrolling == 2) {
      var value = $(".slider").slider("value");
      $(".slider").slider("value", (value + 2));   
   }
}

$(document).ready(function()
{		
   
   setInterval("scrollLeft()", 50);
   setInterval("scrollRight()", 50);
   setTimeout("activate_sliders()", 1000);
      
   $(".prevslide, .nextslide").css("opacity", 0).hover(function () {
      $(this).stop().animate({
         opacity: 1
      }, 50);
      
      if ($(this).hasClass("prevslide")) {
         //terug
         scrolling = 1;
      } else if ($(this).hasClass("nextslide")) {
         //next
         scrolling = 2;
      }
      
   }, function () {
      scrolling = 0;
      $(this).stop().animate({
         opacity: 0
      }, 50);
   });
   
   var ie6container = $(".ie6_notice");
   if ($(ie6container).length > 0) {
      $("#di-visie").remove();
      $(".ie6_notice").css({
         width: "400px",
         height: "400px"  
      });
      return 0;
   }

	Cufon.replace('.logo,.prefix-logo', { fontFamily: 'InterstateBlack'});
   
   $(".image").hover(function () {
      var filename = $(this).find(".metainfo .on").html();
      if (filename.length > 0) {
         $(this).css("background-image", "url(/uploads/images/" + filename + ")");
         $(this).find(".resize_img").attr("src", "/uploads/images/" + filename);
      }
   }, function () {
      var filename = $(this).find(".metainfo .on").html();
      if (filename.length > 0) {
         var filename = $(this).find(".metainfo .off").html();
         $(this).css("background-image", "url(/uploads/images/" + filename + ")");
         $(this).find(".resize_img").attr("src", "/uploads/images/" + filename);
      }
   });   
   
   $(".cycle").cycle({
   	fx:     'fade',
   	speed: '700',
   	timeout: '4000',
   	random: 0,
      prev: '.prev',
      next: '.next'
   });	

   
   
   
   $(".navigatie, .prev, .next, .cycle").disableSelection();
   
   $(".navigatie img").load(function () {
      positionImage($(this));
   });
   
   scaleGrid();
   
   setTimeout("fixCycle", 1);
   
   $("a[rel=external]").each(function () {
      $(this).attr("target", "TOP");
   });
});	

$(window).resize(function() {	
	scaleGrid();
   $(".navigatie img, .navigatie object, .navigatie div").each(function () {
	  positionImage($(this));
   });
   fixCycle();
});

function activate_sliders() {
   $(".slider").each(function () {
      
      var nav_content = $(this).parent().find(".navigatie");
      
      var width = 0;
      $(nav_content).find("img, object, div").each(function () {
         width += $(this).innerWidth();
      });
      
      if (width > $(this).parent().innerWidth()) {
         
         $(this).parent().find(".slider_track").css("display", "block");
         
         $(this).slider({
            slide: function(event, ui) { 
               position_scroll(ui);
            },
            stop: function(event, ui) { 
               position_scroll(ui);
            },
            change: function(event, ui) { 
               position_scroll(ui);
            },
            max: 500,
            min: 0
         }).disableSelection();
      
      } else {
         
         $(this).parent().find(".slider_track, .prevslide, .nextslide").remove();
            
      }
        
   });   
}

function position_scroll(ui) {
   var value = $(ui.value);
   
   if (value.length == 0) {
      value = [0];
   }

   var nav_content = $(ui.handle).parent().parent().find(".navigatie");
   
   var width = 0;
   $(nav_content).find("img, object, div").each(function () {
      width += $(this).innerWidth();
   });
   
   var left = (width - $(ui.handle).parent().parent().innerWidth()) * (parseInt(value[0]) / 500);
   
   $(nav_content).css({
      "left": "-" + left + "px"
   }, 130);   
}

function scaleGrid() {
	var horCols = 12;
	var vertCols = 4;

   var oneColWidthHeight = ($(window).width() / horCols); //($("#grid").innerWidth() / horCols)
	
	$("#grid").css("height", (oneColWidthHeight * vertCols) + "px");
	
   //resize blocks
   $(".block").each(function () {

      var width = 0;
      var height = 0;
      var x = 0;
      var y = 0;
      
      for (var i = 0; i < horCols; i++) {
         if ($(this).hasClass("w" + (i + 1))) {
            width = (i + 1);
         }
      }
      
      for (var i = 0; i < vertCols; i++) {
         if ($(this).hasClass("h" + (i + 1))) {
            height = (i + 1);
         }
      }
      
      for (var i = 0; i < horCols; i++) {
         if ($(this).hasClass("x" + (i + 1))) {
            x = i;
         }
      }
      
      for (var i = 0; i < vertCols; i++) {
         if ($(this).hasClass("y" + (i + 1))) {
            y = i;
         }
      }

       $(this).css({
         width: Math.ceil(width * oneColWidthHeight) + "px",
         height: Math.ceil(height * oneColWidthHeight) + "px",
         top: Math.ceil(y * oneColWidthHeight) + "px",
         left: Math.ceil(x * oneColWidthHeight) + "px"
       });
       
   });
   
   //driehoekjes
   $(".driehoek").each(function () {
      var leftWidth = $(this).css("border-left-width");
      var rightWidth = $(this).css("border-right-width");
      var bottomWidth = $(this).css("border-bottom-width");
      var topWidth = $(this).css("border-top-width");
      
      leftWidth = parseInt(leftWidth.replace("px", ""));
      rightWidth = parseInt(rightWidth.replace("px", ""));
      bottomWidth = parseInt(bottomWidth.replace("px", ""));
      topWidth = parseInt(topWidth.replace("px", ""));
      
      var width = $(this).parent().innerWidth();
      var height = $(this).parent().innerHeight();
      
      if (height > width) {
         width = height;
      }
         
      width = width + 10;
      
      if (leftWidth > 0) {
         $(this).css("border-left-width", width + "px");
      }
      
      if (rightWidth > 0) {
         $(this).css("border-right-width", width + "px");
      }
      
      if (bottomWidth > 0) {
         $(this).css("border-bottom-width", width + "px");
      }
      
      if (topWidth > 0) {
         $(this).css("border-top-width", width + "px");
      }
   });
   
   $("#grid").css("visibility", "visible");
   
	//header resize
	$("#header").css("height", (oneColWidthHeight * 2) + "px");
	$("#header .header-content").css("marginLeft", oneColWidthHeight + "px");
	
	// header logo resize
	$("#header .header-content .logo").css("width", (oneColWidthHeight * 7) + "px");
		$("#header .header-content .logo").css("wordSpacing", "-" + (oneColWidthHeight * 0.05) + "px"); // fix voor _ bij -> what_we_like etc
	$("#header .header-content .prefix-logo").css("width", (oneColWidthHeight * 1) + "px");	
   var hoogteLogovak = $("#header .header-content .logo").height();
	var hoogteLogovakLess = $("#header .header-content .logo").height();
	$("#header .header-content .logo, #header .header-content .prefix-logo").css("fontSize", hoogteLogovak + "px");
	$("#header .header-content .logo div:not(.flashstreep, #streepflash), #header .header-content .prefix-logo div:not(.flashstreep)").css("height", hoogteLogovak + "px");
	$("#header .header-content .logo div:not(.flashstreep, #streepflash)").css("paddingLeft", (hoogteLogovak - 16) + "px");
	Cufon.replace('.logo h1, .prefix-logo', { fontFamily: 'InterstateBlack', fontSize: (hoogteLogovak * 0.8) + 'px' }); /* was (hoogteLogovak-20)  */
	
		if ($.browser.mozilla  && $.client.os != "Mac") {
		 $("#header .header-content .logo .home, #header .header-content .logo .sub").css("marginTop", "-2px"); /* fix jan. logo */
	    }

	
	// header menu resize
	$("#header .header-content .menu").css("marginLeft", (oneColWidthHeight * 1) + "px");
	$("#header .header-content .menu").css("width", (oneColWidthHeight * 7) + "px"); /* = breedte menucontainer */	
	$("#header .header-content .menu").css("paddingTop", (oneColWidthHeight *  0.25) + "px"); /* = paddingTop van menu */	
	$("#header .header-content .submenu").css("top", (oneColWidthHeight * 0.1) + "px"); /* = positie submenu */
	$("#header .header-content .menu a, #header .header-content .submenu a").css("paddingRight", (oneColWidthHeight * 0.2) + "px"); /* = padding knoppen menu/submenu */
	Cufon.replace('#header .header-content .menu, #header .header-content .submenu', { fontFamily: 'InterstateLight', fontSize: (hoogteLogovak * 0.15) + 'px' }); /* was (hoogteLogovak-20)  */
	Cufon.replace('.menu a.on, .submenu a.on', { fontFamily: 'InterstateBlack', fontSize: (hoogteLogovak * 0.15) + 'px' });
	
	// body text resize
	Cufon.replace('.block .txt', { fontFamily: 'InterstateRegular', fontSize: (oneColWidthHeight * 0.12) + 'px' });	
	Cufon.replace('.block .txt em', { fontFamily: 'UtopiaItalic', fontSize: (oneColWidthHeight * 0.12) + 'px' });
	Cufon.replace('.block .txt strong', { fontFamily: 'InterstateBold', fontSize: (oneColWidthHeight * 0.12) + 'px' });
	$(".block .txt").css("lineHeight", (oneColWidthHeight * 0.2) + "px"); /* = lijnhoogte body text */
		/* opdrachtgevers
		Cufon.replace('.block .txt ul li', { fontFamily: 'InterstateRegular', fontSize: (oneColWidthHeight * 0.10) + 'px' });
		$(".block .txt ul li").css("lineHeight", (oneColWidthHeight * 0.12) + "px");
		$(".block .txt ul li").css("marginBottom", (oneColWidthHeight * 0.1) + "px"); */
		 $(".block .txt ul li").css("lineHeight", (oneColWidthHeight * 0.21) + "px"); 
		
	
	// kopjes cufon + resize
	Cufon.replace('h2, h3, h4', { fontFamily: 'InterstateLight', fontSize: (hoogteLogovak * 0.15) + 'px' });	
	$(".title-top").css("top", (oneColWidthHeight * 0.1) + "px"); /* = positie kopje top */	
	$(".title-bottom").css("bottom", (oneColWidthHeight * 0.1) + "px"); /* = positie kopje bottom */
   
   //logo streepje
   $(".flashstreep, .gif, .flashstreep a").css({
      "height": $('#grid').innerHeight() * 0.04 + "px",
      "width": $('#grid').innerHeight() * 0.12 + "px",
      "visibility": "visible"
   });
   
   if ($.browser.mozilla && $.client.os == "Mac") {
     $(".flashstreep").css("top", "60%"); 
     $(".prefix-logo div, .logo div").css("bottom", "-25%");
   } else if ($.browser.mozilla) {
     $(".flashstreep").css("top", "67%"); 
   }
   
   //fix slider
   var new_height = $("#header .header-content .logo img").height();

   /*$(".slider").css({
      height: (new_height / 4) + "px"
   });
   */
   /*
   $(".ui-slider-handle").css({
      height: (new_height / 2) + "px",
      width: (new_width / 2) + "px",
      marginLeft: "-" + (new_width / 4) + "px"
   });
   */   
   
   // padding fix blocks

   $(".block .title-bottom").css({
      bottom: new_height + "px",
      left: new_height + "px"	  
   });    
   $(".block .title-top ").css({
	   top: new_height + "px",
      left: new_height + "px"  
   });  
   $(".block .title-right").css({
      right: new_height + "px"	  
   });
   
   $(".block .txt").css({
      padding: new_height + "px"
   });     
    $(".block .txt-bottom").css({
      paddingTop: (new_height * 2.5) + "px"	  
   });  
   
   Cufon.now();
   //submenu
   $(".menu, .subvisible").css({
      "display": "block"
   });
   var offset = $("#header .menu ul li > a.on").offset(); 
   if (offset) {
      $("#header .header-content .submenu").css("marginLeft", (offset.left - (oneColWidthHeight * 1)) + "px");
   }
}

function fixCycle() {
   $(".cycle img").each(function () {
      var img_height = $(this).innerHeight();
      var parent_height = $(this).parent().parent().innerHeight();
      var marginTop = (parent_height / 2) - (img_height / 2);
      
      $(this).css("margin-top", marginTop + "px");
   });   
}

function positionImage(obj) {
   var img_height = $(obj).innerHeight();
   var container_height = $(obj).parent().parent().innerHeight();
   
   var margin_top = (container_height / 2) - (img_height / 2);
   
   $(obj).css("margin-top", margin_top + "px");
}

function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<li><span>'+status+'</span></li>');
  }
  document.getElementById(username + '_update_list').innerHTML = statusHTML.join('');
}

(function() {
	
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.userAgent,
				subString: "iPhone",
				identity: "iPhone/iPod"
		    },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
	
	};
	
	BrowserDetect.init();
	
	window.$.client = { os : BrowserDetect.OS, browser : BrowserDetect.browser };
	
})();
