/**
 * Loads the max-width property of an image
 * @function $( selector ).image_max_size()
 */

(function(){
	$.fn.image_max_size = function (max, elt, max_width, max_height) {
		if(!max)max="100%";
		if($.ie6) 
		{
			elt = this;
			max_width = this.width();
			max_height = this.height();
			
			elt.hide();
			
			elt.css('width', max);
		
			
			if(max_width < elt.width())elt.width(max_width);
			
			elt.css('opacity',0).show();	
			elt.animate({
				opacity: 1
			});
			
		}
		else this.css('max-width', max).animate({
			opacity: 1
		});
		
	};
})(jQuery);

(function(){
	$.fn.portlet_portfolios=function(e) {	
		var width = this.width();	
		var data = this.find(".galery_view object");
		if(!data.length) return this;					
		if(width<370){									
			data.css("width", 240).css("height", 180).attr("data", data.attr("data").replace("_mid.xml", "_tn.xml"));
		}
		else{
			data.css("width", 380).css("height", 300).attr("data", data.attr("data").replace("_tn.xml", "_mid.xml"));
		}
		return this;
	};
})(jQuery);

(function(){
	$.fn._IZ=$.fn.image_zoom=function(opt) {
		
		$.fn._IZ.tpl_max_width_add = 30;
		$.fn._IZ.tpl_max_height_add = 35;
		$.fn._IZ.text_width = opt.text && opt.text.replace(/\s*/,"") != "" ? 150:0;
		$.fn._IZ.tpl_space = opt.space ? opt.space:50;		
		
		if($.fn._IZ.open) return $.fn._IZ.close();		
		if(!$.fn._IZ.tpl) {
			$.fn._IZ.tpl = $("<div id='image_zoom'></div>").css({
				position: $.browser.msie?'absolute':'fixed',
				left: -99999, top: 0, zIndex: 99999999
			}).appendTo("body");
			$.fn._IZ.closer = $("<a class='close'></a>").appendTo($.fn._IZ.tpl).bind("click",$.fn._IZ.close);
			$.fn._IZ.title = $("<h1 class='title'></h1>").appendTo($.fn._IZ.tpl);			
			$.fn._IZ.text = $("<p></p>").appendTo($.fn._IZ.tpl);
			$.fn._IZ.ovr = $("<div id='image_zoom_overlay' style='background:black;'>").css({
				position: $.browser.msie?'absolute':'fixed', width: "100%", height: "100%",
				opacity: 0, top: 0, left: 0, zIndex: 99999998
			}).appendTo("body");
		}
		$.fn._IZ.tpl.css({ width:'auto', height:'auto' });		
		if($.fn._IZ.img) $.fn._IZ.img.remove();
		$.fn._IZ.img = $("<img class='image' align='left'>").bind("click",$.fn._IZ.close).css({ width:'auto', height:'auto' });;	
		
		/* */ if(!$.fn._IZ.text_width) $.fn._IZ.img.css({ border:"0px" });
		
		$.fn._IZ.text.before($.fn._IZ.img);
		$.fn._IZ.start_left = this.offset().left;
		$.fn._IZ.start_top = this.offset().top;
		$.fn._IZ.start_width = this.width();
		$.fn._IZ.start_height = this.height();
		$.fn._IZ.text.html(opt.text ? opt.text: "");
		$.fn._IZ.title.html(opt.title ? opt.title: "");
		$.fn._IZ.ovr.css({display:"block", opacity:0});	
		/* */ $.hideembed();
		var url = this.attr("href")? this.attr("href"):(opt.url?opt.url:null);
		if(!url) return this;
		$.fn._IZ.img.attr("src", url);
		$.fn._IZ.first = true;
		if(!$.fn._IZ.img[0].complete || !$.fn._IZ.img.width() )$.fn._IZ.img[0].onload = $.fn._IZ.load;
		else $.fn._IZ.load();
		$.fn._IZ.open = true;
		return this;
	};
	$.fn._IZ.close = function() {
		$.fn._IZ.open = false;
		if($.browser.msie)clearTimeout($.fn._IZ.ie6_int);
		$.fn._IZ.tpl.css({left: -99999});
		$.fn._IZ.ovr.animate({opacity:0}, 100, function() {$(this).hide();});
		$(window).unbind("resize", $.fn._IZ.resize);
		$.fn._IZ.ie6_int = null;
		/* */ $.showembed();
		return this;
	};
	$.fn._IZ.load = function() {
		$.fn._IZ.ie6_scroll = $.browser.msie ? document.documentElement.scrollTop: window.pageYOffset;				
		$.fn._IZ.img_max_width = $.fn._IZ.img.width();
		$.fn._IZ.img_max_height = $.fn._IZ.img.height();	
		$.fn._IZ.ratio = $.fn._IZ.img_max_width / $.fn._IZ.img_max_height;
		$.fn._IZ.img_min_width = Math.min(200, $.fn._IZ.img_max_width);
		$.fn._IZ.img_min_height = $.fn._IZ.img_min_width*(1/$.fn._IZ.ratio);
		$.fn._IZ.tpl.css({
			width:$.fn._IZ.start_width,
			left: $.fn._IZ.start_left,top: $.fn._IZ.start_top - ($.browser.msie?0:$.fn._IZ.ie6_scroll )
		});		
		$.fn._IZ.img.css({width:$.fn._IZ.start_width, height:$.fn._IZ.start_width*(1/$.fn._IZ.ratio)});	
		$.fn._IZ.resize();
		
		$(window).bind("resize", $.fn._IZ.resize);
		if($.browser.msie && !$.fn._IZ.ie6_int) {
			$.fn._IZ.ovr.height($(window).height() + document.documentElement.scrollTop);
			$.fn._IZ.ie6_resize();
		}
		
	}
	$.fn._IZ.ie6_resize = function() {
		if(document.documentElement.scrollTop != $.fn._IZ.ie6_scroll) {
			$.fn._IZ.ie6_scroll = document.documentElement.scrollTop;
			$.fn._IZ.tpl.animate({ top:$.fn._IZ.ie6_tpl_top + $.fn._IZ.ie6_scroll}, 100);
			$.fn._IZ.ovr.height($(window).height() + document.documentElement.scrollTop);
		}
		if($.fn._IZ.open)$.fn._IZ.ie6_int = setTimeout($.fn._IZ.ie6_resize, 20);
	}
	$.fn._IZ.resize = function() {
		if($.ie6)$.fn._IZ.ovr.height($(window).height() + document.documentElement.scrollTop);
		var win_width = $(window).width()- ($.fn._IZ.tpl_space*2);
		var win_height = $(window).height()- ($.fn._IZ.tpl_space*2);		
		var img_width = Math.max(Math.min(win_width - $.fn._IZ.text_width - $.fn._IZ.tpl_max_width_add,$.fn._IZ.img_max_width - $.fn._IZ.tpl_max_width_add),$.fn._IZ.img_min_width);
		var img_height = img_width * (1/$.fn._IZ.ratio);
		if(img_height > $.fn._IZ.img_max_height || img_height > win_height - $.fn._IZ.tpl_max_height_add ){
			
			img_height = Math.max(Math.min(win_height - $.fn._IZ.tpl_max_height_add,$.fn._IZ.img_max_height - $.fn._IZ.tpl_max_height_add),$.fn._IZ.img_min_height);
			img_width = img_height * $.fn._IZ.ratio;
		}
		//if(img_width >= $.fn._IZ.img_max_width - $.fn._IZ.tpl_max_width_add) {
		//	var tpl_height = img_height + $.fn._IZ.tpl_max_height_add;
		//}
		//else {
			var tpl_height = img_height + $.fn._IZ.tpl_max_height_add;
			var tpl_width = img_width + $.fn._IZ.text_width;// + $.fn._IZ.tpl_max_width_add;		
			var tpl_left = (win_width+($.fn._IZ.tpl_space*2)-tpl_width) / 2;
			var tpl_top = (win_height+($.fn._IZ.tpl_space*2)-tpl_height) / 2;
		//}			
		$.fn._IZ.title.css({ top: -$.fn._IZ.title.height() -5 })
		$.fn._IZ.ie6_tpl_top = tpl_top;
		$.fn._IZ.n_img_height = img_height;
		$.fn._IZ.n_tpl_top = tpl_top;
		$.fn._IZ.img.stop().animate({width:img_width, height:img_height},100, function() {
			
			if($.fn._IZ.first){				
				$.fn._IZ.ovr.animate({opacity:0.3}, 80);
				$.fn._IZ.first = false;
			}
		});
		$.fn._IZ.tpl.css({ overflow: 'visible' }).stop().animate({left:tpl_left, top:tpl_top+($.browser.msie?$.fn._IZ.ie6_scroll:0),width:tpl_width}, 100, function() {
			$.fn._IZ.title.css({ top: -$.fn._IZ.title.height() -5 })
			/*var test = ($.fn._IZ.tpl.height() + $.fn._IZ.tpl.offset().top) - $(window).height();
			console.log($.fn._IZ.max_reached + " -"  + test)
			if( test > 0 && $.fn._IZ.max_reached) {
				var n_t = $.fn._IZ.tpl.offset().top - test;
				var n_l = $.fn._IZ.tpl.offset().left; 
				var n_w = $.fn._IZ.tpl.width();
				if(n_t<0) {
					n_t=0;
					n_w += test/2;
					n_l-= test/2;
				}
				$.fn._IZ.tpl.animate({top: n_t, left: n_l, width:n_w }, 350);
			}*/
		});		
	}
})(jQuery);
