// JavaScript Document
$(document).ready(function(){
	
	//INITIAL TAG CLOUD STATE
	//ProductCloud.init();
	Referenties.init();
	
	//SHOWROOM GALLERY
	showRoomGallery.init();
	
	//PRODUCT DETAIL
	ProductDetail.init();

    ProductSearch.init();

});

var ProductSearch = {

    init: function(){
        $( '.boxSearch' ).bind( 'click', function(){
            $(this).attr( 'value', '' );
        });

        $( '.boxSearch' ).bind( 'blur', function(){
            var val = $(this).attr( 'value' );
            if( val == '' ){
                $(this).attr( 'value', 'merk of product zoeken' );
            }
        });
    }

}

/**
 * Product Cloud Object
 */
var ProductCloud = {
	
	init: function()
	{
		ProductCloud.tagHandlers();
	},
	
	tagHandlers: function()
	{
		$('.tabStop').bind( 'click', ProductCloud.clicker );
	},
	
	clicker: function()
	{
		var href = $(this).attr( 'href' );
		var cleanHref = href.split( '.' );
		var productItem = cleanHref[0].split( '-' );
		
		( productItem[1] != undefined ) ? type = productItem[1] : type = productItem[0];

		ProductCloud.ajaxHandler( type );
		$( '.tabStop' ).removeClass( 'activeWhite' );
		$( this ).addClass( 'activeWhite' );
		return false;
	},
	
	ajaxHandler: function( type )
	{
		$.post( 'ajax/product.html', { soort: type }, function( data ){
			$('#productsHolder').html( data );											   
		});
	}
	
}

/**
 * ProductDetail
 */
var  ProductDetail = {
	
	init: function()
	{
		ProductDetail.clickHandlers();
	},

    currentProduct: null,

	clickHandlers: function()
	{
		/*
         *  Geen bind gebruikt, omdat je een parameter nodig hebt.
         */
        $( '.productPrev' ).click(function(){
            ProductDetail.clickEvent( 'prev' );
            return false;
        });

        $( '.productNext' ).click(function(){
            ProductDetail.clickEvent( 'next' );
            return false;
        });
	},
	
	clickEvent: function( e )
	{
        var url = window.location;

        var urlString = url.toString();

        urlString = urlString.replace( /\.html/, "" );

        var aParts = urlString.split( /\// );

        var iCount = aParts.length;

        ProductDetail.ajaxHandler( aParts[iCount -1], e );

        return false;
	},

    ajaxHandler: function( sUrl, sAction )
    {
        sUrl = ( ProductDetail.currentProduct == null ) ? sUrl : ProductDetail.currentProduct;

        var sCategorie = null;

        $( '.tabStop' ).each( function(){
            if( $(this).hasClass('activeWhite') ){
                var arrClasses = $(this).attr('class').split( ' ' );
                sCategorie = arrClasses[0];
            }
        });

        $.post( 'ajax/volgendproduct.html', { sProduct: sUrl, sActie: sAction, sCategorie: sCategorie }, function(data){
            ProductDetail.xmlParse( data );
        });
        return false;
    },

    xmlParse: function( data )
    {
        if( $("id", data).text() != 'null' ){
           ProductDetail.currentProduct = $("url", data).text();

           $('#productDetail .column-two').html( $("content", data).text() );

           if( $("website", data).text() != '' ){
               var sHTML = '<a class="grey" href="' + $("website", data).text() + '" target="_blank">Ga naar de website van de fabrikant</a>';
               $('#productDetail .column-two').append( sHTML );
           }
        }
    }
	
}

/**
 * Referenties
 */
var Referenties = {
	
	init: function()
	{
		Referenties.clickHandlers();
	},
	
	clickHandlers: function()
	{
		$('.tabStopRef').bind( 'click', Referenties.clickFunction );
	},
	
	clickFunction: function()
	{
		var href = $(this).attr( 'href' );
		var cleanHref = href.split( '.' );
		var refItem = cleanHref[0].split( '-' );
		
		( refItem[1] != undefined ) ? type = refItem[1] : type = refItem[0];
		
		Referenties.ajaxHandler( type );
		$('.tabStopRef').removeClass( 'activeReferentie' );
		$(this).addClass( 'activeReferentie' );
		return false;	
	},
	
	ajaxHandler: function( type )
	{
		$.post( 'ajax/referentie.html', { referentieType: type }, function( data ){
			$('.referentieContent').html( data );
            startBox();
            //tb_init('a.thickbox, area.thickbox, input.thickbox');
		});
	}
	
} 

/**
 * Showroom
 */
var showRoomGallery = {
	
	init: function()
	{
		showRoomGallery.clickHandlers();
		
		$( '.ajaxContent' ).ajaxStart( function(){
			$( '.ajaxContent' ).prepend( '<div class="showRoomLoad" style=""><img src="img/ajaxloader.gif" width="220" height="19" alt="loading..." /></div>' );
		}).ajaxStop( function(){
			$( '.showRoomLoad' ).remove();
		});
	},
	
	clickHandlers: function()
	{
		$( '.gallery' ).bind( 'click', showRoomGallery.clickEvent );
	},
	
	clickEvent: function()
	{
		var rawAction = $(this).attr( 'class' );
		var action = rawAction.split( ' ' );
		var image = $( '.galleryImage' ).attr( 'src' );
		
		showRoomGallery.ajaxHandler( action[1], image );
		return false;	
	},
	
	ajaxHandler: function( action, image )
	{
		$.post( 'ajaxdummy.php', { actie: action, plaatje: image }, function( data ){
			var newImage = 'images/' + data;
			$( '.galleryImage' ).attr( 'src', newImage );
		});
	}
} 
