// SHOTSHOP JS EVENTs ... 2010-01-25

function init_toolbox() {
	var $toolbox_image = $('.toolbox_image');
	$toolbox_image.draggable( {
		revert : 'invalid', // when not dropped, the item will revert back to
		// its initial position
		scope : 'can_be_added',
		helper : 'clone',
		start : function(e, ui) {

		}
	});
}

function init_page_specials() {
	$('a.link').click(function() {
		if ($(this).attr('href') === 'abo-newsletter') {
			register_newsletter();
		}

		return false;
	});
}

function init_tooltip(image_id) {
	$tooltip = $('#tooltip_image');

	// log(image_id);
	source = $tooltip.attr('src');
	// log(source);

	$tooltip.draggable( {
		revert : 'invalid',
		scope : 'can_be_added',

		helper : function() {
			var $clone = $('#' + image_id);
			$clone.attr('ref', image_id);

			return $clone.clone();
		},

		zIndex : 1000,
		cursorAt : {
			top : 50,
			left : 50

		},
		start : function(e, ui) {
			global_tooltip_can_close = 0;
		},
		drag : function(e, ui) {
		},
		stop : function(e, ui) {
			global_tooltip_can_close = 1;
			$("#tooltip").css("display", "none");
		}
	});
}

function init_gallery() {
	var mousex = 0;
	var mousey = 0;
	var $gallery = $('#gallery'), $lightbox = $('#lightbox'), $cart = $('#cart'), $thumbnail = $('.thumbnail_image');

	var $lightbox_compact = $('#lightbox_view_compact');
	var $lightbox_extended = $('#lightbox_view_extended');
	$selection = $('.selection_image');

	$selection.draggable( {
		revert : 'invalid', // when not dropped, the item will revert back to
		// its initial position
		scope : 'can_be_added',
		helper : 'clone',
		zIndex : 1000
	});

	$thumbnail.draggable( {
		revert : 'invalid', // when not dropped, the item will revert back to
		// its initial position
		scope : 'can_be_added',
		helper : 'clone',
		zIndex : 1000,

		start : function() {
			global_show_tooltip = 0;
			$("#tooltip").css("display", "none");
		},
		drag : function() {
			$("#tooltip").css("display", "none");
		},
		stop : function() {
			global_show_tooltip = 1;
		}
	});

	$lightbox_compact.sortable( {
		start : function(e) {
			$(this).css("cursor", "move");
			global_show_details = 0;
			$(global_LB_rollover).css( {
				'visibility' : 'hidden'
			});
		},
		stop : function(ev) {
			$(this).css("cursor", "pointer");
		},
		update : function(ev, ui) {
			var result = $lightbox_compact.sortable('toArray');
			sort_lightbox(result);

		}
	});

	$lightbox_extended.sortable( {
		start : function(e) {
			global_show_details = 0;
			$(this).css("cursor", "move");
		},
		update : function(ev, ui) {
			var result = $lightbox_extended.sortable('toArray');
			sort_lightbox(result);
		},
		stop : function(e) {
			$(this).css("cursor", "pointer");

		}
	});

	// let the lightbox be droppable, accepting the gallery items
	$lightbox.droppable( {
		scope : 'can_be_added',
		tolerance : 'pointer',
		hoverClass : 'drop_hover',
		// activeClass: 'drop_hover',
		drop : function(ev, ui) {
			add_to_lightbox($(this), ui.draggable);
		}
	});

	// let the cart be droppable, accepting the gallery items
	$cart.droppable( {
		scope : 'can_be_added',
		tolerance : 'pointer',
		hoverClass : 'drop_hover',
		// activeClass: 'drop_hover',
		drop : function(ev, ui) {
			add_to_cart($(this), ui.draggable);
		}
	});

	// let the gallery be droppable as well, accepting items from the trash
	/*
	 * $gallery.droppable({ accept: '#cart li', activeClass:
	 * 'custom-state-active', drop: function(ev, ui) {
	 * recycleImage(ui.draggable); } });
	 */

	function add_to_lightbox($this, $item) {
		var $list = $this;
		var my_id = $item.attr('ref');
		imageposition = $('#lightbox_images')
		$item.clone().prependTo(imageposition).fadeIn(function() {

			$(this).animate( {
				width : '40px'
			}).find('img').animate( {
				height : '40px'
			});
		});

		update_lightbox(my_id);
		// log(my_id);
	}

	function add_to_cart($this, $item) {
		var $list = $this;
		var my_id = $item.attr("ref");
		// alert(my_id);
		imageposition = $('#cart_images')

		$item.clone().prependTo(imageposition).fadeIn(function() {
			$(this).animate( {
				width : '40px'
			}).find('img').animate( {
				height : '40px'
			});
		});

		update_cart(my_id);
	}

	$("#tooltip_boundary").mouseleave(function(e) {
		if (global_tooltip_can_close == 1) {
			$("#tooltip").css("display", "none");
		}
	});

	$('.helpwindow').click(function(e) {
		pagename = $(this).attr('href');
		show_help(pagename);
		return false;
	});

	// $('.abowindow').onclick(function(e) {
	// register_newsletter();
	// return false;
	// });

	$('#language_selector').mouseleave(function(e) {
		$("#language_selector").css("display", "none");
	});

	$('#browse_selector').mouseleave(function(e) {
		$("#browse_selector").css("display", "none");
	});

	$('#logo').click(function() {
		redirect('/');
	});

	$('.fp_teaser').mouseenter(function(e) {
		var teaser = $(this).attr("id");
		show_me = '#fph_' + teaser.replace(/\D+/g, '');
		hide_me = '#fpt_' + teaser.replace(/\D+/g, '');

		$(show_me).css("display", "block");
		$(hide_me).css("display", "none");
	}).mouseleave(function(e) {
		var teaser = $(this).attr("id");
		show_me = '#fpt_' + teaser.replace(/\D+/g, '');
		hide_me = '#fph_' + teaser.replace(/\D+/g, '');

		$(show_me).css("display", "block");
		$(hide_me).css("display", "none");
	});

	$('.thumbnail').mouseenter(function(e) {
		if (global_show_tooltip == 1) {
			// console.log('mouseenter // global_show_tooltip = ' +
			// global_show_tooltip);
			clearTimeout(global_timeout);

			var offsetx = 0;
			var offsety = 0;

			var lupe_width = 295;
			var lupe_height = 327;

			var imageid = $(this).attr("id");
			var position = $(this).position();
			var wrapper_pos = $("#middle-wrapper").offset();
			var wrapper_width = $("#body-wrapper").css("width");

			var div_width = $(this).css("width");
			var div_height = $(this).css("height");

			var thumb_width = div_width.replace(/\D+/g, '');
			var thumb_height = div_height.replace(/\D+/g, '');
			var wrapper_width = wrapper_width.replace(/\D+/g, '');

			thumb_width = parseInt(thumb_width);
			thumb_height = parseInt(thumb_height);
			wrapper_width = parseInt(wrapper_width);

			position.left += Math.round(thumb_width / 2);
			position.top += Math.round(thumb_height / 2);

			position.top += wrapper_pos.top - Math.round(lupe_height / 2);
			position.left += wrapper_pos.left - Math.round(lupe_width / 2);

			if (position.top <= Math.round(thumb_height / 2)) {
				position.top = Math.round(wrapper_pos.top / 2);
			}

			if (position.left <= Math.round(thumb_width / 2) - 20) {
				position.left = Math.round(wrapper_pos.left / 2) - 20;
			}

			if (position.left > wrapper_width - lupe_width + 30) {
				position.left = wrapper_width - lupe_width + 30;
			}

			$("#tooltip").css("display", "none");
			global_id = imageid;
			global_left = Math.round(position.left);
			global_top = Math.round(position.top);
			global_timeout = setTimeout("tooltip()", 150);
			global_watch_timeout = 1;
		}
	}).mouseleave(function(e) {
		if (global_show_tooltip == 1) {
			clearTimeout(global_timeout);
			global_watch_timeout = 0;
		}
	});
	
	$('#middle').mousemove(function(e) {
		if (global_watch_timeout == 0) {
			$("#tooltip").css("display", "none");
		}
	});
};

