jQuery(document).ready(function(){
	var images = jQuery("a[rel='popup']")
	var win_h = jQuery(window).height()
	var win_w = jQuery(window).width()
	var image_pop = jQuery(".image-pop")
	var image_pop_cont = jQuery(".image-pop-content")
	var image_pop_border = 10;
	var site_opacity = jQuery("#site-opacity")
	
	function close_img_pop () {
		image_pop.animate({"opacity": "0"}, 500)
		image_pop.hide()
		site_opacity.hide()
		image_pop[0].setAttribute("style",'')
	}
	jQuery(image_pop).find(".close").click(function(){
		close_img_pop ()
	})
	images.click(function(){
		if (jQuery.browser.msie && jQuery.browser.version <= 6) {
			msie_detected = true
			jQuery("body").css("height",jQuery(document).height())
		} else {
			msie_detected = false
		}
		site_opacity.show()
		var pos_top = jQuery(this).offset().top
		
		image_pop[0].setAttribute("style",'')
		image_pop.css("opacity","0").show()
		var is_load = false
		function load_image (obj) {
			is_load = true
			var w = jQuery(obj).width()
			var h = jQuery(obj).height()
			if (h > win_h || msie_detected) {
				image_pop.css({"position":"fixed","top":"10px"})
				image_pop.css({"position":"absolute","top":jQuery(document).scrollTop() - (0 - 10) + 'px'})
				var mgn = "0 0 0 -"+(w/2 + image_pop_border)+"px"
			} else {
				image_pop.css({"position":"fixed","top":"50%"})
				var mgn = "-"+(h/2 + image_pop_border)+"px 0 0 -"+(w/2 + image_pop_border)+"px"
			}
			image_pop_cont.width(w)
			image_pop_cont.height(h)
			image_pop.css("margin",mgn)
			image_pop.animate({"opacity": "1"}, 500)
		}
		image_pop_cont.html(
			jQuery("<img>").attr({"src":jQuery(this).attr("href"),"name":"popup"}).load(function(){
				if (!is_load) {
					load_image(jQuery("img[name='popup']"))
				}
			}).click(function(){
				close_img_pop ()
			})
		)
		if (jQuery.browser.msie) {
			if (!is_load) {
				load_image(jQuery("img[name='popup']"))
			}
		}
		
		return false
	})
})
