//layout 
$.fn.layOut = function(){
	if (browser.winIE){
		$('.rnd, .msgbox').corner("8px");
		$('.rndsmall').corner("4px");
		//$('.rndlink').corner("4px"); //errors when applied on links?
		$('input:checkbox, input:radio').css({ border: "none", background: "none" });
	}
};

//toggle input value
$.fn.toggleVal = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

//toggle div
$.fn.toggleDiv = function(text1, text2, class1, class2) {
	return this.toggle(function(){
		var el = $('.' + this.href.split('#')[1]);
        $(this).text(text2);
        $(this).removeClass(class1);
        $(this).addClass(class2);
       	el.animate({ height: 'toggle', opacity: 'toggle' }, "slow");
    },function(){
		var el = $('.' + this.href.split('#')[1]);
        $(this).text(text1);
        $(this).removeClass(class2);
        $(this).addClass(class1);
        el.animate({ height: 'toggle', opacity: 'toggle' }, "slow");
    });
};

//open external links in new window
$.fn.externalLinks = function() {
	var localDomain = (location.href.split('/')[2]);
	var localExtensionsInNewWindow = Array('doc','xls','pdf');
	var list = document.getElementsByTagName('A');
	for (var i = 0; i < list.length; i++){
	var aEl = list[i];
	var aElHref = aEl.href;
	// check for extensions
	var extension = (aElHref.substring(aElHref.length - 4,aElHref.length)).toLowerCase();
	var foundExtension = false;
	for (var j = 0; j < localExtensionsInNewWindow.length; j++){
		if (extension == '.' + localExtensionsInNewWindow[j]){
			foundExtension = true;
		break;
		}
	}
	// open in new window if conditions are right
	if ((aElHref.split('/')[2] != localDomain && aElHref.indexOf('mailto:') == -1 && aElHref.indexOf('javascript:') == -1) || foundExtension){
		$(aEl).addClass('external').attr('title', 'external link');
		aEl.onclick = function(){
			window.open(this.href);
			return false;
			}
		}
	}
};

//Round border
$.fn.roundBorder = function(){
	if (browser.winIE6Down){
		$(".tablist ul li a").prepend('<span class="tl"></span>').append('<span class="tr"></span>');
	} else {
		$(".rnd, .mask, .tablist ul li a").prepend('<span class="tl"></span>').append('<span class="tr"></span>');
		$(".rnd, .mask").prepend('<span class="bl"></span>').append('<span class="br"></span>');
	}
	//$(".rnd").prepend('<div class="ml"></div>').append('<div class="mr"></div>');
	//$(".rnd").prepend('<div class="tm"></div>').append('<div class="bm"></div>');
};

/* semantical Xite colors */
var XiteColors = {
	yellow		:'#dde200', 
	lime		:'#bdc232', 
	lightgrey	:'#555a5d', 
	red			:'#e00034', 
	orange		:'#ff5b00', 
	blue		:'#00b9e4', 
	pink		:'#ea3bae',
	white		:'#ffffff', 
	black		:'#000000'
};
var getXiteColor = function(color){
	if(XiteColors[color]){
		return XiteColors[color];
	}
	return color;
}

/**
 * Animate text-hover colors on a jQuery collection
 * @param {String} 	overColor
 * @param {String} 	outColor
 * @param {Integer} duration
 * @param {Boolean} queue
 */
$.fn.hoverColor = function(overColor, outColor, duration, queue){
	overColor	= getXiteColor(overColor);
	outColor	= getXiteColor(outColor);
	queue		= Boolean(queue);
	duration	= (duration && typeof duration == 'number' && duration > 100 ? duration : 400 );
	$(this).hover(
		function(){ $(this)
			.animate( {color:overColor}, {queue:queue, duration:duration} )
		}, 
		function(){ $(this)
			.animate( {color:outColor}, {queue:queue, duration:duration} )
		}
	);
}

 /**
  * Animate text-hover colors on a jQuery collection
  * @param {String} 	overColor
  * @param {String} 	outColor
  * @param {Integer} duration
  * @param {Boolean} queue
  */
 $.fn.hoverBackgroundColor = function(overColor, outColor, duration, queue){
	 overColor	= getXiteColor(overColor);
	 if(overColor == 'transparent'){
		 overCss = { opacity:0 }
	 }else{
		 overCss = { backgroundColor:overColor, opacity:1 }
	 }
	 outColor	= getXiteColor(outColor);
	 if(outColor == 'transparent'){
		 outCss = { opacity:0 }
	 }else{
		 outCss = { backgroundColor:outColor, opacity:1 }
	 }
	 queue		= Boolean(queue);
	 duration	= (duration && typeof duration == 'number' && duration > 100 ? duration : 400 )
	 $(this).hover(
			 function(){ $(this)
				 .animate( overCss, {queue:queue, duration:duration} )
			 }, 
			 function(){ $(this)
				 .animate( outCss, {queue:queue, duration:duration} )
			 }
	 );
 }
 
/** 
 * Animate text & border-hover colors on a jQuery collection of elements
 * @param {String} 	overColor
 * @param {String} 	outColor
 * @param {Integer} duration
 * @param {Boolean} queue
 */
$.fn.hoverBorderColor = function(overColor, outColor, duration, queue){
	overColor	= getXiteColor(overColor);
	outColor	= getXiteColor(outColor);
	queue		= Boolean(queue);
	duration	= (duration && typeof duration == 'number' && duration > 100 ? duration : 400 )
	$(this).hover(
		function(){ $(this)
			.animate( {color: overColor}, {queue: queue, duration: duration} )
			.animate( {borderLeftColor: overColor}, {queue: queue, duration: duration} )
			.animate( {borderRightColor: overColor}, {queue: queue, duration: duration} )
			.animate( {borderTopColor: overColor}, {queue: queue, duration: duration} )
			.animate( {borderBottomColor: overColor}, {queue: queue, duration: duration} );
		}, 
		function(){ $(this)
			.animate( {color: outColor}, {queue: queue, duration: duration} )
			.animate( {borderLeftColor: outColor}, {queue: queue, duration: duration} )
			.animate( {borderRightColor: outColor}, {queue: queue, duration: duration} )
			.animate( {borderTopColor: outColor}, {queue: queue, duration: duration} )
			.animate( {borderBottomColor: outColor}, {queue: queue, duration: duration} );
		}
	);
}
 
/* assign color-animations on document ready */
$(document).ready(function(){
	$(".icon-arrow-pink, .pinklinks a, a.pinklink").hoverColor("pink","lightgrey");
	$(".icon-arrow-red, .redlinks a, a.redlink").hoverColor("red","lightgrey");
	$(".icon-arrow-orange, .orangelinks a, a.orangelink").hoverColor("orange","lightgrey");
	$(".icon-arrow-blue, .bluelinks a, a.bluelink").hoverColor("blue","lightgrey");
	$(".icon-arrow-yellow, .yellowlinks a, a.yellowlink").hoverColor("yellow","lightgrey");
	$(".whitelinks a, a.whitelink").hoverColor("white","lightgrey");
	$("input.button, a.button, a.rndlink, a.rndlinkbig").hoverBorderColor("blue","lightgrey");
	$("a.rndlink.red, a.rndlinkbig.red").hoverBorderColor("red","lightgrey");
	$("a.rndlink.yellow, a.rndlinkbig.yellow").hoverBorderColor("yellow","lightgrey");
//	$("a.myXite.rndlink").hoverComplexColor({border:["yellow","yellow"], background:["transparent","lime",'.bg'], text:["yellow","black", '.txt']});
	$("a.myXite.rndlink.yellow").hoverBorderColor("yellow","yellow");
//	$("a.myXite.rndlink.yellow .bg").hoverBackgroundColor("transparent","lime");
//	$("a.myXite.rndlink.yellow .txt").hoverColor("yellow","black");
	$("a.rndlink.pink, a.rndlinkbig.pink").hoverBorderColor("pink","lightgrey");
});

//autoscroll to anchor (confirms to className from anchor)
$(document).ready(function(){
	$(".autoscroll").click(function () { 
		var el = $('.' + this.href.split('#')[1]);
		$.scrollTo( el, 800, { easing:'easeOutQuad' });
		return false;
	});
	
});


// lazy rollover with preload
$(document).ready( function(){
   lazy.rollover.init();
});
lazy = {};
lazy.rollover = {
   init: function() {
      this.preload();
      $(".rollover").hover(
         function () { $(this).attr( 'src', lazy.rollover.newimage($(this).attr('src')) ); }, 
         function () { $(this).attr( 'src', lazy.rollover.oldimage($(this).attr('src')) ); }
      );
   },
   preload: function(){
      $(window).bind('load', function() {
         $('.rollover').each( function( key, elm ) { $('<img>').attr( 'src', lazy.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   newimage: function( src ){ 
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_o' + src.match(/(\.[a-z]+)$/)[0]; 
   },
   oldimage: function( src ){ 
      return src.replace(/_o\./, '.'); 
   }
};

/**
 * remove a field from the set of iterated user_link fields in my-profile form
 * @param {Object} evt	click event normally
 */
function removeUserLink(el){
	var parts = el.attr('id').split('_');
	var index = parts[parts.length-1];
	var recEl = $('#UserLink_record_'+index);
	var recLabel = $('#UserLink_label_'+index).attr('value');
	if(confirm('"'+recLabel+'" verwijderen?')){
		recEl.remove();
	}
}

/**
 * add a field to the iterated user_link fields in my-profile form
 * @param {Object} el	clicked +url button normally
 */
function addUserLink(el){
	var parts = el.attr('id').split('_');
	var index = Number(parts[parts.length-1]);
	var collEl = $('#UserLink_collection');
	var recEl = $('#UserLink_record_'+index);
//	if(confirm('Jouw website/kanaal toevoegen aan je account?')){
		var newIndex = index+1;
		if (index == 0) { // there are no records, insert one
			collEl.html(newUserLinkRecord(newIndex));
		}else{ // add a record after the last one
			var newRecEl = recEl.after(newUserLinkRecord(newIndex));
		}
		$('#remove_UserLink_'+newIndex).click(function(event){
			removeUserLink($(this));
			event.preventDefault();
		});
		$('#add_UserLink_'+newIndex).click(function(event){
			addUserLink($(this));
			event.preventDefault();
		});
		
		if($('#add_UserLink_'+index)) 		$('#add_UserLink_'+index).hide();
		if($('#remove_UserLink_'+index)) 	$('#remove_UserLink_'+index).show();
//	}
}

function newUserLinkRecord(index){
	return '\n'
		+'<div id="UserLink_record_'+index+'" class="clearright">\n'
//		+'	<input id="UserLink_id_'+index+'" class="left mright auto" type="hidden" size="45" name="UserLink['+index+'][id]" value="" style="display:none;" />\n'
		+'	<label>naam van website (bijvoorbeeld: mijn youtube kanaal)</label>\n'
		+'	<input id="UserLink_label_'+index+'" class="left mright auto" type="text" size="45" name="UserLink['+index+'][label]" value="" />\n'
		+'	<label>url (bijvoorbeeld: http://www.youtube.com/user/mijnYouTubeKanaal)</label>\n'
		+'	<input id="UserLink_url_'+index+'" class="left mright auto" type="text" size="45" name="UserLink['+index+'][url]" value="" />\n'
		+'	<a  id="remove_UserLink_'+index+'"class="remove_UserLink left button mright" href="#" title="Site verwijderen" style="display:none;">- url</a>\n'
		+'	<a id="add_UserLink_'+index+'" class="add_UserLink left button" href="#" title="Nog een site toevoegen">+ url</a>\n'
		+'	<div class="clearfix">&nbsp;</div>\n'
		+'</div>\n';
}

// FontReplacement
//, sWmode : "transparent" or , sWmode : "opaque"
$.fn.do_sIFR = function(){
if(typeof sIFR == "function"){
		sIFR.replaceElement("h1.whiteDin, h2.whiteDin, h3.whiteDin, h4.whiteDin, h5.whiteDin", "/fonts/DinRegular.swf", named({sColor : "#FFFFFF", sBgColor:"#000000", sHoverColor:"#FFFFFF", sWmode : "transparent"}));
		sIFR.replaceElement("h1.greyDin, h2.greyDin, h3.greyDin, h4.greyDin, h5.greyDin", "/fonts/DinRegular.swf", named({sColor : "#555a5d", sBgColor:"#000000", sHoverColor:"#555a5d", sWmode : "transparent"}));
		sIFR.replaceElement("h1.pinkDin, h2.pinkDin, h3.pinkDin, h4.pinkDin, h5.pinkDin", "/fonts/DinRegular.swf", named({sColor : "#EA3BAE", sBgColor:"#000000", sHoverColor:"#EA3BAE", sWmode : "transparent"}));
/* _DinRegular.swf with readability-aliasing  will not work for multiline text */
//		sIFR.replaceElement("#ContentBody h3.pinktxt", "/fonts/_DinRegular.swf", named({sColor : "#EA3BAE", sBgColor:"#000000", sHoverColor:"#EA3BAE", sWmode : "transparent"}));
//		sIFR.replaceElement("#ContentBody h3", "/fonts/DinRegular.swf", named({sColor : "#EA3BAE", sBgColor:"#000000", sHoverColor:"#EA3BAE", sWmode : "transparent"}));
		sIFR.replaceElement("h1.blueDin, h2.blueDin, h3.blueDin, h4.blueDin, h5.blueDin", "/fonts/DinRegular.swf", named({sColor : "#00b9e4", sBgColor:"#000000", sHoverColor:"#00b9e4", sWmode : "transparent"}));
		sIFR.replaceElement("h1.redDin, h2.redDin, h3.redDin, h4.redDin, h5.redDin", "/fonts/DinRegular.swf", named({sColor : "#e00034", sBgColor:"#000000", sHoverColor:"#e00034", sWmode : "transparent"}));
		sIFR.replaceElement("h1.orangeDin, h2.orangeDin, h3.orangeDin, h4.orangeDin, h5.orangeDin", "/fonts/DinRegular.swf", named({sColor : "#ff5b00", sBgColor:"#000000", sHoverColor:"#ff5b00", sWmode : "transparent"}));
		sIFR.replaceElement("h1.yellowDin, h2.yellowDin, h3.yellowDin, h4.yellowDin, h5.yellowDin", "/fonts/DinRegular.swf", named({sColor : "#dde200", sBgColor:"#000000", sHoverColor:"#dde200", sWmode : "transparent"}));
	}
};


//DomLoad by Dean Edwards edited by Robert Cherny and Matthias Miller for https use.
//http://www.cherny.com/webdev/26/domloaded-object-literal-updated
var DomLoaded ={
	onload: [], loaded: function(){
	if (arguments.callee.done) return; arguments.callee.done = true;
	for (i = 0;i < DomLoaded.onload.length;i++) DomLoaded.onload[i](); }, load: function(fireThis) { this.onload.push(fireThis);
	if (document.addEventListener) { document.addEventListener("DOMContentLoaded", DomLoaded.loaded, null); };
	if (/KHTML|WebKit/i.test(navigator.userAgent)){ var _timer = setInterval(function(){ if (/loaded|complete/.test(document.readyState)){ clearInterval(_timer); delete _timer; DomLoaded.loaded();}}, 10);};
	/*@cc_on @*/
	/*@if (@_win32)
	var proto = "src='javascript:void(0)'"; if (location.protocol == "https:") proto = "src=//0";
	document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() { if (this.readyState == "complete") { DomLoaded.loaded(); } };
	/*@end @*/
   window.onload = DomLoaded.loaded; }
};

//DomLoaded.load(init);
DomLoaded.load(function() {

	$('html').layOut();
	$('html').do_sIFR();
	$('html').roundBorder();
	$('html').externalLinks();
	$('.tablist > ul').tabs({ fx: { opacity: 'toggle' } });
	$('.slide > ul').tabs({ fx: { opacity: 'toggle' } });
	
	$('.togval').toggleVal();
	if (browser.safari) { $('input[type=file]').css({ backgroundColor: "transparent", border: "none", color: "#555a5d"}); };
	
	$('a.toggle').toggleDiv('meer opties','minder opties','','');
	$('a.togplusmin').toggleDiv(null,null,'plus','min');
	
	
	//if (browser.safari) {$('input[@name=search]').attr({ type:'search', placeholder:'search', autosave:'nl.xite', results:'8'});}s
	if(browser.winIE) {try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {};}
	
});



