function reset_(){
	$('input:not(.yname)').val('');
}
function langMenu(){
	$('.langMenu').append('<div class="title"></div>');
	$('.langMenu a').hover(function(){
		var TITLE = $(this).find('img').attr('title');
		var offset = $('.langMenu').offset();
		$('.langMenu .title').html(TITLE);
		
		$('.langMenu .title').css({top:(offset.top-20),left:(offset.left),width:$('.langMenu').width()});
		$('.langMenu .title').show();
		
	},function(){
		$('.langMenu .title').hide();
	});
}
$(function(){
//	$(window).resize(_remover_all);
	langMenu();
	$('.ffsubmit').click(function(){
		if( validateFriendFunction() ){
			$('.friend-function').fadeOut(function(){ ajax_loader_start('.ffWrap'); });
			$.post('index.php?main_page=lightbox&action='+$('input[name=action][type=hidden]').val(),{name:$('.yname').val(),fname:$('.fname').val(),femail:$('.femail').val()},mailSent);
		}
		return false;
	});
	
	function validateFriendFunction(){
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if(  ! $('.yname').val() ){
			alert( $('.yname').attr('alt') );
			$('.yname').focus();
			return false;
		}
		if( $('.fname').attr('alt') && ! $('.fname').val() ){
			alert( $('.fname').attr('alt') );
			$('.fname').focus();
			return false;
		}
		if( ! filter.test($('.femail').val()) ){
			alert( $('.femail').attr('alt') );
			$('.femail').focus();
			return false;
		}
		
		return true;
	}
	
	function mailSent(){
		ajax_loader_stop();
		$('.friend-function').fadeIn();
		var obj = swfobject.getObjectById("myCom"); 
		if (obj && typeof obj.gogo != "undefined") {
			obj.gogo();
		}
		return false;
	}
	
	var _body = 'body';
	var base_url =  'http://jolaby.co.uk/';
	
	var overlay = '.overlay ';
	var overlay_code = '<div class="overlay"></div>';
	var ieoverlay = 'overlay-ie';
	var macoverlay = 'overlay-ie';
	var product = '.product ';
	
	var remove_data = new Array();
	
	var _navigator = '.navigator ';
	var _navigator_code = '<div class="navigator"></div>';
	
	var _description = '.description ';
	var _description_code = '<div class="description maximized"></div>';
		
		var _description_minimize = '.description_minimize ';
		var _description_minimize_code = '<a href="#" class="description_minimize" title="minimize">-</a>';
		var _description_content = '.description_content ';
		var _description_content_code = '<div class="description_content"></div>';
	
	var loader_anim = '.loader_anim';
	var loader_anim_code = $('<div class="loader_anim"></div>');

	var close_modal = '.close_modal';
	var close_modal_code = $('<a href="#close" class="close_modal" title="close"></a>');

	var big_image = '.big_image ';
	var big_image_code = '<div class="big_image"></div>';

	
	var image_thumbs = '.image_thumbs ';
	var image_thumbs_code = '<ul class="image_thumbs"></ul>';

	var maximized = 'maximized';
	var minimized = 'minimized';

	var shadow_select = '.fx-shadow';
	
	var dsc = 'div.dsc ';
	var additonal_images = 'ul.additonal-images ';
	
	var ajax_loader = '.ajax_loader';
	var ajax_loader_code = '<div class="ajax_loader"></div>';
	
	var in_view = 'in_view';
	var not_in_view = 'not_in_view';
	
	var light_box_dom = '.light-box';
	
	var image_listing = 'a.light-box-image';
	
	var navigator_created = false;
	
	var add_description = true;
	var add_navigator = true;
	var show_image_flag = false;
	
	var press_listing = ' .press-listing ';

jfr_init();
disable_right_click();
function disable_right_click(){
	function clickIE4(){
		if (event.button==2){ return false;	}
	}
	function clickNS4(e){
		if (document.layers||document.getElementById&&!document.all){
			if (e.which==2||e.which==3){ return false; } 
		}
	}	
	if (document.layers){
		document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown=clickNS4;
	}else if (document.all&&!document.getElementById){
		document.onmousedown=clickIE4;
	}	
	document.oncontextmenu=new Function("return false");
}

function jfr_init(){
	 $('.category-title, .ifr').flash(
        { 
            src: base_url+'jfr.swf', 
            flashvars: { 
                css: [
                    '* { color: #aeaeae; }',
                    'a { color: #aeaeae; text-decoration: none; }',
                    'a:hover { text-decoration: underline; }'
                ].join(' ')
            }
        },
        { version: 7 },
        function(htmlOptions) {
            htmlOptions.flashvars.txt = this.innerHTML;
            this.innerHTML = '<div>'+this.innerHTML+'</div>';
            var $alt = $(this.firstChild);
            htmlOptions.height = $alt.height()+5;
            htmlOptions.width = $alt.width();
            $alt.addClass('alt');
			$alt.hide();
            $(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));						
        }
    );
	
}

dragable_init();
$.each( $(product), function(){
	$(this).find('a').addClass('remove_product');
//	$(this).find('.remove_product').css('top', $(this).offset().top );
//	$(this).find('.remove_product').css('left', $(this).offset().left );
});
$(product).find('img').click(function(){
		ovelay_init();
		light_box( $(this).parent() );
		return false;
});

if(!navigator_created){
	$.each( $(image_listing), function(){
		$(this).click(function(){
			ovelay_init();
			add_description = true;
			add_navigator = false;
			show_image_flag = true;
			light_box( $(this) );
			
			return false;
		});
	});
}


var current_drag;
		
	function dragable_init(){

		$.each( $('.dragablethumb'), function(){ 

			$(this).draggable({ 
						   	helper : 'clone',
							opacity : 0.5,
							start : function(ev, ui){
									ui.helper.css('cursor', 'move');
									current_drag = ui.helper;
								}
						   });
		} );
		
		$(light_box_dom).droppable({
			accept: ".dragablethumb",
			activeClass: 'droppable-active',
			hoverClass: 'droppable-hover',
			drop: function(ev, ui) {
				$id = $( current_drag ).attr('id');
				id = $id.substring( $id.indexOf('[')+1, $id.indexOf(']') );
				ajax_loader_start(light_box_dom);
					$.post('index.php?main_page=lightbox&action=add&product_id='+ id, function(msg){ 
																					$(light_box_dom).html(msg); 
																					
																					ajax_loader_stop(); 
																				});
				}
		});
		
	}
	
	function ajax_loader_start($where){
		$($where).append(ajax_loader_code);
		$(ajax_loader).hide();
		_top_ = ( $($where).height() / 2 ) + $($where).offset().top;
		_left_ = ( $($where).width() / 2 ) + $($where).offset().left;
		$(ajax_loader).css('top', _top_);
		$(ajax_loader).css('left', _left_);
		$(ajax_loader).fadeIn();
	}
	
	function ajax_loader_stop(){
		$(ajax_loader).fadeOut('slow');
		$(ajax_loader).remove();
	}
	
	function light_box($this){
		loader_init();
		_append(_body, loader_anim, loader_anim_code);
		_append(_body, big_image, big_image_code);
		
		if( add_description ){
			_append(_body, _description, _description_code);
			_append(_description, _description_minimize, _description_minimize_code);
			_append(_description, _description_content, _description_content_code);
			$(_description).hide();
		}
		if (add_navigator) {
			_navigator_init($this);
		}
		if(show_image_flag){
			show_image($this);
		}
	}
	
	function modal_close_init(){
		_append(overlay, close_modal, close_modal_code);
		remove_data.push(close_modal);
		$(close_modal).css('left', ( $(overlay).width() - $(close_modal).width() ) + 'px' );
		$(close_modal).css('top', '0px' );
		$(close_modal).unbind('click');
		$(close_modal).click(_remover_all);
	}
	
	
	function _description_init(){
		var tmp_z_index;
		$(_description).hover	(
								function(){
									tmp_z_index = $(this).css('z-index');
									$(this).css('z-index', '10000');
								},
						  		function(){
									$(this).css('z-index', tmp_z_index);
								}
							);
		
	}
	
	function add_dsc_content($this){
		$(_description).show();
		var maximized_height = 20;
		var maximized_width = 250;
		var maximized_top = 150 + $(document).scrollTop();
		var maximized_left = 	( ($(_body).width() / 8 ) - ( maximized_width / 2 ) );
		
		$(_description).animate({
										width: maximized_width + 'px',
										height: maximized_height + 'px',
										top : maximized_top + 'px',
										left : maximized_left + 'px'
								    },500, function(){
											if(add_navigator){
												$(_description_content).html( $this.find(dsc).html() );
											}else{
												$(_description_content).html( $this.parent().parent().parent().find('td:last').html() );
											}
											$(_description).animate({ height : ($(_description_content).height() + 20 )+'px'},200);
											$(_description_minimize).click(function(){ return false; });
											bind_min_max();
								    });
	
	function bind_min_max(){
		$(_description_minimize).unbind('click');
				$(_description_minimize).click(function(){
												if( $(_description).hasClass(maximized) ){
												do_minimize();
												return false;
				}
											
				if( $(_description).hasClass(minimized) ){
												do_maximize();
												return false;
				}
		});
		
	}
	function do_minimize(){
			var minimized_height = $(_description_minimize).height();
			var minimized_width = $(_description_minimize).width();
			var minimized_top =   $(_navigator).html() != null ? $(_navigator).offset().top - minimized_height - 20  : $(_description).offset().top;

			var minimized_left = 2;
			
			$(_description_content).hide();
				
			$(_description).animate({	width: minimized_width + 'px',
									height: minimized_height + 'px',
									top : minimized_top + 'px',
									left : minimized_left + 'px'
								}, 500, function(){ 
									
									$(_description).removeClass(maximized).addClass(minimized);
								} );
		}
	function do_maximize(){
				
				$(_description).animate({
										width: maximized_width + 'px',
										height: maximized_height + 'px',
										top : maximized_top + 'px',
										left : maximized_left + 'px'
								    }, 500, function(){
									    $(_description_content).show();
									    $(_description).animate({ height : ($(_description_content).height() + 20 )+'px'},200);
									    $(_description).removeClass(minimized).addClass(maximized);
									});
		
		}
		}
	
	
	function _navigator_init($this){
		navigator_created = true;
		add_dsc_content($this);
		$id = $this.find('.thumb').attr('id');
		id = $id.substring( $id.indexOf('[')+1, $id.indexOf(']') );
		
		_append(_body, _navigator,_navigator_code);
		
		$(_navigator).css('top', ( $(window).height()+ $(document).scrollTop() - $(_navigator).height() - 2 ) + 'px' );
				_append(_navigator, image_thumbs, image_thumbs_code);
				start_loading();
				$(image_thumbs).load('index.php?main_page=gallery_image&get_additional_images=true&gallery_image_id='+id,function(){ core_init_navigator() });
				
	}
	
	function core_init_navigator(){
		
		$(_navigator+' img').bind('load', function(){
				stop_loading();
				$.each( $(_navigator).find(image_thumbs).find('li > a'), 	function(){
																	$(this).click( function(){ return show_image( $(this) )  } );
																} );
				
				
				$(_navigator).animate({ top : ( $(window).height()+ $(document).scrollTop() - $(_navigator).height() - 2 ) + 'px' }, 200, function(){ _description_init(); show_image( $(image_thumbs + 'li:first a') ); });
		});
	}
	
	function loader_init(){
		$(loader_anim).hide();
	}
	
	function start_loading(){
		$(loader_anim).show();
		if (add_navigator) {
			called_top_tmp = $(_navigator).offset().top + $(document).scrollTop();
			called_top_tmp = called_top_tmp ? called_top_tmp : 2;
			called_top_tmp /= 2;
		}else{
			called_top_tmp = ($(window).height()/2) + $(document).scrollTop() ;
		}
		var image_top = ( called_top_tmp - ($( loader_anim ).height() / 2) );
		var image_left = ( ($(_body).width() / 2) - ($( loader_anim ).width() / 2) );
		$( loader_anim ).css('left', image_left + 'px' );
		$( loader_anim ).css('top', '-' + $( loader_anim ).height() + 'px' );
		$( loader_anim ).animate({ top : image_top + 'px'}, 500);
	}
	
	function stop_loading(){
		$(loader_anim).hide();
	}
	
	function show_image($this){
		if( $this.find('img').hasClass(in_view) && navigator_created ){ return false; }
		
		$(shadow_select).remove();
		$.each( $(image_thumbs).find('a img'), function(){
										$(this).addClass(not_in_view).removeClass(in_view);
									} );
		
		$this.find('img').addClass(in_view).removeClass(not_in_view);
		
		
		var _href = $this.attr('href');
		var _title = $this.attr('title');

		
		$( big_image ).hide();
		$( big_image ).html('<img src="'+ _href +'" alt="'+ _title +'" />')
		
		var big_img_img = $( big_image ).find('img');
		start_loading();
		big_img_img.bind('load', function(){
								stop_loading();
								
								if( $(_navigator).html() != null ){
									if( $(_navigator).offset().top-$(document).scrollTop() < $(big_image).height() ){
										t_height = $(big_image).height();
										t_width = $(big_image).width();
										n_height = ($(_navigator).offset().top - $(document).scrollTop())-20;
										n_widht = ((n_height*t_width)/t_height );
										$(big_image).find('img').width(n_widht);
										$(big_image).find('img').height(n_height);

									}
								}else{
									add_dsc_content($this);
								}
								if (add_navigator) {
									
									var image_top = (($(_navigator).offset().top / 2) + ($(document).scrollTop()/2) - ($(big_image).height() / 2));
								}else{
									if( $(window).height() < $(big_image).height() ){
										n_height = ( $(window).height() )-20;
										n_widht = ((n_height*$(big_image).width())/$(big_image).height() );
										$(big_image).find('img').width(n_widht);
										$(big_image).find('img').height(n_height);
									}
									var image_top = $(document).scrollTop() + ( $(window).height()/2 ) - ($(big_image).height() / 2);
								}
								var image_left = ( ($(_body).width() / 2) - ($( big_image ).width() / 2) );
								$( big_image ).css('left', image_left + 'px' );
								$( big_image ).css('top', '-' + $( big_image ).height() + 'px' );
								$(big_image).show();
							//	offset_shadow = 3;
								//if($.browser.msie && $.browser.version < 7){ offset_shadow = 1; }
								$( big_image ).animate({ top : image_top + 'px'}, 500, function(){
										
										__left = $( big_image ).offset().left + $( big_image ).width() + 30;
										__top = 0;
										__top = $(big_image+' img ').offset().top - $(document).scrollTop();
										$(close_modal).animate({
											left:__left,
											top : __top
										},500);
										
								});
								
								
								remove_data.push(shadow_select);
							});
		return false;
	}
	
	
	
	function _append($where, $what, $what_code ){
		$($where).append($what_code);
		remove_data.push($what);
	}
	
	function _html($where, $what_code){
		$($where).append($what_code);
	}
	
	function _remover_all(){
		$( remove_data.join(', ') ).animate({  top : '-'+$(overlay).height()+'px' }, 500,  function(){ $(this).remove(); });
		$("body").css('overflow', 'auto');
		return false;
	}
	
	function ovelay_init(){
		_append(_body , overlay, overlay_code);
		$("body").css('overflow', 'hidden');
		$('body, html,'+overlay).css({height: '100%', width: '100%'});
		
		if( $.browser.msie && $.browser.version < 7 ){
			
			
			$(overlay).addClass(ieoverlay);
		}
		
		if( $.browser.safari ){
			//$(overlay).addClass(macoverlay);
		}
		$(overlay).click(_remover_all);
		modal_close_init();
	}
	html_tidy();
	function html_tidy(){
		$('input:text, input:password, textarea').addClass('text-input');
	}
	
	
});


