//TomS(18-9-2009): Extension op JQuery voor uitlezen van querystring t.b.v. van laden Cowbox a.d.h.v. bijv. ".../?id=1411"
jQuery.extend({
    'Q' : window.location.search.length <= 1 ? {}
        : function(a){
            var i = a.length, 
                r = /%25/g,  // Ensure '%' is properly represented 
                h = {};      // (Safari auto-encodes '%', Firefox 1.5 does not)
            while(i--) {
                var p = a[i].split('=');
                h[ p[0] ] = r.test( p[1] ) ? decodeURIComponent( p[1] ) : p[1];
            }
            return h;
        }(window.location.search.substr(1).split('&'))
});

$(document).ready(function(){
// var dStart=new Date, dEnd;
	var $document = $(this);
	$('.scrollbar').jScrollPane({showArrows:true});


	//TOMS->11nov09-> Cufon test
	//Cufon.replace('.custom_headers', {font-size:'17px'}); 
	Cufon.replace('.custom_headers');
	Cufon.now(); 


	var imagesDir = 'fileadmin/thecowcompany/templates/images/buttons/';
	$('input[type=radio]').checkize({
		checked: imagesDir+'checked.gif',
		checked_down: imagesDir+'checked_down.gif',
		checked_hover: imagesDir+'checked_hover.gif',
		
		unchecked: imagesDir+'unchecked.gif',
		unchecked_down: imagesDir+'unchecked_down.gif',
		unchecked_hover: imagesDir+'unchecked_hover.gif'
	});

	//TOMS->14sept09->voor tonen van producten in een Lightbox
	if ($(window.top.document).find('.jquery-lightbox').length==0 && $(window.top.document).find('#header').length==0)
	{
		document.location= "/?cowboxid="+$.Q.id;
	}
	
	//TOMS->Als er ID bekend is van de page/product/cowbox
	if($.Q.cowboxid){
		$document.cowbox({pageId: $.Q.cowboxid, doktype: 761}); 
	}
	//TOMS->Als er meerdere 
	if($.Q.search){	
		$document.find("#tx_mhajaxsearch_qq").attr('value',$.Q.search);
		$document.cowbox({pageId: 17, mode: 'page'});
		//$(this).cowbox({pageId: $.Q.cowboxid, doktype: 761}); 
	}

	//TOMS->22sept09->Voor toevoegen van gele verschuifknop, zodat de csshover.htc werkt 
	if($.browser.msie && $('#moveRight').length==0)
	{
		$('#ie-6').after('<div id="moveRight"></div>'); //after body?
	}
	
	if($.Q.afdeling == "verkoop"){
		alert($.Q.afdeling);
		$.cowAjax({pageId: 60, mode: 'workers', pivar: 'group', unit: 47}); 
		//	$document.cowbox({pageId: $.Q.cowboxid, doktype: 761}); 
	}
	

	$('input[type=text]:not(.imp-text,.i-text)').each(function(){
		$(this).before('<img src="fileadmin/thecowcompany/templates/images/blocks/bg_input_l.gif" alt="" height="17" style="margin-bottom: -4px;" />');
		$(this).after('<img src="fileadmin/thecowcompany/templates/images/blocks/bg_input_r.gif" alt="" height="17" style="margin-bottom: -4px;" />');
	});
    $('select').each(function(){
		$(this).before('<img src="fileadmin/thecowcompany/templates/images/blocks/bg_input_l.gif" alt="" height="20" style="margin-bottom: -3px;" />');
		$(this).after('<img src="fileadmin/thecowcompany/templates/images/blocks/bg_input_r.gif" alt="" height="20" style="margin-bottom: -3px;" />');
	});

	if($.browser.msie || $.browser.mozilla) {
		$('textarea').each(function(){
			if($.browser.msie) {
				imageMargin = '-4';
			} else {
				$(this).parent().hasClass('tail-box-content') ? imageMargin = '-3' : imageMargin = '-2';
			}
			$(this).before('<img src="fileadmin/thecowcompany/templates/images/blocks/textarea_left.gif" alt="" height="' + $(this).outerHeight()/*this.clientHeight*/ + '" style="margin-bottom: ' + imageMargin + 'px;" />');
			$(this).after('<img src="fileadmin/thecowcompany/templates/images/blocks/textarea_right.gif" alt="" height="' + $(this).outerHeight()/*this.clientHeight*/ + '" style="margin-bottom: ' + imageMargin + 'px;" />');
		});
	}

	//TomS(7-sep-09): Thema menus. List-style image aanpassen
	var counter =1;
	$('div.fce_2cols_68x23').find('.navigation.navigation-icons').find('li').each(function(){
			var image= 'thema_menu_li'+ (counter++) +'.gif';
			$(this).css({background: 'url(fileadmin/thecowcompany/templates/images/bullets/' + image + ') no-repeat', height: '20px'}); 
	});
	
	//TomS(31-aug-09): Gedeelte uitgebreid, zodat de titel en de navigatieknoppen
	//				   groen worden als je erover heen gaat met je muis
	//North ->>
	
	$('div.tx-impcarousel-pi1').bind('mouseover',function(){
		$this = $(this);
		$this.prev().prev().addClass('act');
		$this.parent().prev().prev().addClass('act');
		$this.find('.carrusel_left a').addClass('act');
		$this.find('.carrusel_right a').addClass('act');
	});
	$('div.tx-impcarousel-pi1').bind('mouseout',function(){
		$this = $(this);
		$this.prev().prev().removeClass('act');
		$this.parent().prev().prev().removeClass('act');
		$this.find('.carrusel_left a').removeClass('act');
		$this.find('.carrusel_right a').removeClass('act');
	});
	
	$('.csc-header').bind('mouseover,mouseout',function(){
		$this = $(this).next().next();
		$this.find('.carrusel_left a').toggleClass('act');
		$this.find('.carrusel_right a').toggleClass('act');
	});

	$('.image-grey-color').hide();

	$("div.fce_round, div.news-churn-wraper .popup-box").bind('mouseover',function(){
        $this = $(this);
		
		$this.find(".small_tab").addClass('act');
		$this.find("a.png-fix").css({backgroundPosition: 'right bottom'});
		$this.find("span.png-fix").css({backgroundPosition: 'right top'});
		
		$this.find("div.fce-item > div.img-holder img").parent().each(function(){
                     var $el  = $(this);
                     if($el.hasClass('image-grey-color')){
			$el.prev().hide();
                        $el.show();
		     };
		});
	});
	
	$("div.fce_round, div.news-churn-wraper .popup-box").bind('mouseout',function(){
		$this = $(this);
		$this.find(".small_tab").removeClass("act");
		$this.find("a.png-fix").css({backgroundPosition: 'left bottom'});
		$this.find("span.png-fix").css({backgroundPosition: 'left top'});
		
		$this.find("div.fce-item > div.img-holder img").parent().each(function(){
			$el = $(this);
			if ($el.hasClass('image-grey-color')) {
				$el.prev().show();
				$el.hide();
			}
		});
	});

	//TomS (23-juli-2009): hover-event gemaakt voor image zonder reflection (afgekeken bij image-grey-functie)
	
	$('.image-hover-active').parent().hide();

    $('.image-hover').bind('mouseover',function(){
		$this = $(this).parent();
		if($this.next().children('image-hover-active')){
			$this.hide();
			$this.next().show();
		}
	});
	$('.image-hover-active').bind('mouseout',function(){
		$this = $(this).parent();
		if($this.prev().children('image-hover')){
			$this.hide();
			$this.prev().show();
		}
	});
	
	$('.fce-product-wrap').parent().parent().addClass('fce_nopadding');
	
	//---------------------------------------------------------------------------------------------------------
	// TomS-> PPP-logo een link geven, omdat dit via de template niet werkt
//North->>
/*	$('.ppp_link').each(function(){
		$(this).attr("href","?id=22");
	});
*/
	$('.ppp_link').attr("href","?id=22");
	
//	dEnd=new Date;
//  $('body').append('<div class="debug">'+(dEnd-dStart)+"</div>");

});


function switchTabs(obj,aClass,isRight){
	$obj = $(obj).parent();
	
	if (isRight && !$obj.find(".tabs_right").is(":visible")){
		$obj.find(".tabs_left").css("display","none");
		$obj.find(".tabs_right").css("display","block");
		$obj.find(".tabs_left iframe").css("display","none");
		$obj.find(".tabs_right iframe").css("display","block");
		$obj.find("."+aClass).toggleClass("act");
	}
	if (!isRight && !$obj.find(".tabs_left").is(":visible")){
		$obj.find(".tabs_right").hide();
		$obj.find(".tabs_left").show();
		$obj.find(".tabs_right iframe").hide();
		$obj.find(".tabs_left iframe").show();
		$("."+aClass).toggleClass("act");
	}
}

displayForm = function (elementId)
{
	var content = [];
	$('#' + elementId + ' input').each(function(){
		var el = $(this);
		if ( (el.attr('type').toLowerCase() == 'radio'))
		{
			if ( this.checked )
				content.push([
					'"', el.attr('name'), '": ',
					'value="', ( this.value ), '"',
					( this.disabled ? ', disabled' : '' )
				].join(''));
		}
	});
	alert(content.join('\n'));
}