//$.blockUI( { backgroundColor: '#f00', color: '#fff'} );

$().ajaxStart(function() { /* $( '#loading_page' ).show() */ } ).ajaxStop(function() { /* $( '#loading_page' ).hide() */ } );

$( document ).ready( function()
{
	$( 'ul#nav li a' ).each( function() {
		if( $( this ).attr( 'href' ) == current_url || $( this ).attr( 'href' ) == complete_url ) 
		{
			$( this ).parent().addClass( 'active' );
		}
	} );
	
	$( '#search_formular .submit_text' ).click( function() {
	
		var button_search = $( this ).attr( 'name' );
		
		var search_value = $( '#' + button_search + '_width' ).val();

		if( search_value == 0 )
		{
			alert( 'Please select width!' );
		}
		else
		{
			document.location = '/search/' + button_search + '/params/width/' + search_value;
		}
	} );
	
	if( popup_question ) { $( '#popup_question' ).show(); }
	
	if( popup_question_ok )	{ alert( 'Thank you for your message' ); }
	
	$( '.zebra' ).zebra();
	
	ajaxProduct();
	
	/**
	 * A warning message appears everytime you click an object in the page which has
	 * a warning class attached to it
	 */
	$( '.warning' ).click( function()
	{
		if( ! confirm( 'Are you sure? This cannot be undone!' ) )
		{
			return false;
		}
		
		return true;
	});
});

function openWindow( theURL, winName, features )
{
	window.open( theURL, winName, features );
}

function ajaxProduct()
{
	$( '#product_list a' ).bind( 'click', function( e ) {
			$.scrollTo( '#logo', 0 );
			$( '#page' ).load( $( this ).attr( 'href' ) + '/ajax', function() { ajaxProduct(); } );
			return false;
	} );
	
	$( '#prod_name a' ).bind( 'click', function( e ) { $( '#page' ).load( $( this ).attr( 'href' ) + '/ajax', function() { ajaxProduct(); } ); return false; } );
	
	$( '.close_popup_question' ).click( function() { $( '#popup_question' ).hide(); return false; } );
	$( '.show_popup_question' ).click( function() { $( '#popup_question' ).show(); return false; } );
	
	$( '#photos #thumbs a' ).click( function() {
		
		var image = $( this ).attr( 'rel' );
		var title = $( 'img', this ).attr( 'title' );
		
		var img = new Image();
		img.onload = function() { showLabel(); };
		img.src = '/files/products/medium_' + image;
		
		$('#photo a img').attr( 'src', '/files/products/medium_' + image );
		$('#photo a img').attr( 'title', '' );
		$('#photo a img').attr( 'title', title );
		var href_print = $('#photo a').attr( 'href' ) + '/image/' + image;
		$('#photo a').attr( 'href', href_print );
		$('#enlarge a').attr( 'href', href_print );
	
		showLabel();
		
		return false;
	} );
	
	$( '#enlarge a' ).click( function() { openWindow( $( this ).attr( 'href' ), '', 'scrollbars=yes,width=840,height=760' ); return false; } );
	
	$( '#photo a' ).click( function() { openWindow( $( this ).attr( 'href' ), '', 'scrollbars=yes,width=840,height=760' ); return false; } );
	
	$( 'a.email_product' ).click( function() { openWindow( $( this ).attr( 'href' ), '', 'scrollbars=yes,width=450,height=450' ); return false; } );
	
	showLabel();
}

function showLabel()
{
	$( '#photo div' ).remove();
	var height = $( '#photo a img' ).height();
	var width = $( '#photo a' ).width();
	var off_top = ''
	var off_left = '';
	$( '#photo a img' ).each( function() {
		off_top = this.offsetTop;
		off_left = this.offsetLeft;
	} );
	
	var label_text = $( '#photo img' ).attr( 'title' );
	
	if( label_text )
	{
		var label_html = '<div style="position: absolute;left: ' + off_left + 'px; top:' + ( height - 30 ) + 'px; width: ' + width + 'px">' + label_text + '</div>';
		$( '#photo' ).append( label_html );
	}
}
