//declare all globals
var img = [];




//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//emulate popup function...
var popWindow = function(u,w,h,popEl){
	if(!popEl){
		e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		if(e == 'png' || e == 'gif' || e == 'jpg')
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>",{overlay:20});
		else
			popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\"  width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>",{overlay:20});
	}
	popEl.modal({
			onOpen: function (dialog) {
						dialog.data.show();
						dialog.data.addClass('alert');
						dialog.container.css('height','auto');
						dialog.overlay.fadeIn('normal', function () {
							dialog.container.show('normal', function () {
								//dialog.data.slideDown('normal'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						//dialog.data.slideUp('normal', function () {
						  dialog.container.hide('normal', function () {
							dialog.overlay.fadeOut('normal', function () {
							  $.modal.close(); // must call this to have SimpleModal
											   // re-insert the data correctly and
											   // clean up the dialog elements
							});
						  });
						//});
					}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
}


//animation
var start_image_scroll = function(){
	x = $(".photoalbum_thumbs span:eq(0)").width();
	$(".photoalbum_thumbs").animate({'left':(x * -1) + 'px'}, Math.round(x * 20),"linear",function(){
		$(".photoalbum_thumbs span:eq(0)").appendTo(".photoalbum_thumbs");
		$(".photoalbum_thumbs").css('left','0px');
		start_image_scroll();
	});
}




$(document).ready(function(){


/*
	//menu rollover	
	var zIndex = 2;
	$("#menu ul li").hover(
		 function(){
			 if($(this).children('ul:eq(0)').is(':hidden')){
				  $(this).children('ul:eq(0)').css('z-index',zIndex++).slideDown('normal');
			 }
		 },
		 function(){
			 if($(this).children('ul:eq(0)').is(':visible')){
				  $(this).children('ul:eq(0)').slideUp('normal');
			 }
		 }
	);
*/

	$(".albumsets .set").click(function(){
		self.location.href = $(this).find('a').attr('href');
	}).hover(
		function(){$(this).addClass('over');},
		function(){$(this).removeClass('over');}
	);
	
	//photoalbum
	if($(".photoalbum_module").length){

		$(".photoalbum_module .photoalbum_thumb a").click(function(e){
			e.preventDefault();
			$('*').css('cursor','wait');

			var href = $(this).attr('href');
			i = href.match(/photoID=([0-9]+)/);
			a = href.match(/albumID=([0-9]+)/);

			$.getJSON('/modules/photoalbums/index.php',{photoID:i[1],albumID:a[1],json_photo_data:1},function(data){
				data['photo']['path'] = '/' + data['photo']['path'];

				var id = 'img_' + data['ID'];
				if(!img[id]){
					img[id] = new Image();
				}
				img[id].onload = function(){
					$('*').css('cursor','');
					
					if(!$("#simplemodal-data").length){
						obj = $('<div />');
					}else{
						obj = $("#simplemodal-data");
					}
					obj.css({
						backgroundImage:'url(' + data['photo']['path'] + ')',
						width:data['dimensions'][0],
						height:data['dimensions'][1]
					}).html('<div class="nav"><div class="nav left"></div><div class="nav right"></div></div>' + (data['photo']['info'] ? '<div class="info">' + data['photo']['info'] + '</div>' : ''));
					obj.find('.nav').fadeTo(0,0.5).height(data['dimensions'][1]);
					obj.hover(
						function(){
							$(this).find('.nav').fadeTo('normal',1);
							i = $(this).find('.info');
							h = i.outerHeight() * -1;
							i.animate({marginTop:h},'normal');
						},
						function(){
							$(this).find('.nav').fadeTo('normal',0.5);
							i = $(this).find('.info').animate({marginTop:0},'normal');
						}
					);
					obj.find('.nav.right').click(function(){
						$('#photoalbum_thumb_' + data['next'] + ' a').trigger('click');
					});
					obj.find('.nav.left').click(function(){
						$('#photoalbum_thumb_' + data['prev'] + ' a').trigger('click');
					});
					if(!$("#simplemodal-data").length){
						popWindow('',data['dimensions'][0],data['dimensions'][1],obj);
					}
				}
				img[id].src = data['photo']['path'];
			});

			return false;
		});

		//start_image_scroll();
		
	}
	
	
	$('#left .submenu_module').parents('.article').find('div.title').remove();

});
