$(document).ready(function(){
	simple_form($(this));						   
});
var expand;

function _open(a_link) {
	

	var code, size, img;

	img = a_link.children();
	
	_build(img);
		
	//_show(100, 100);
		
	imgPreloader = new Image();
	imgPreloader.src = a_link.attr("href");
	imgPreloader.onload = function(){

		size = _fitToViewport(imgPreloader.width, imgPreloader.height);
		
		code = '<div class="margin"><div class="top_bar"><div class="popupPager"></div><a href="#" class="close">&nbsp;</a><a href="#" class="btnexpand">&nbsp;</a><a href="#" class="btncontract">&nbsp;</a></div><div class="popupImage"><img src="' + a_link.attr("href") + '" id="main_img" width="'+ size['width'] +'" height="'+ size['height'] +'"/></div></div>';

		$("#info").html(code);
		
		$("div.popupPager").html($("div.pager").html());
		
		$("div.popupPager a").each(function(){
			var splt = $(this).attr("href").split("thumb_");
			$(this).attr("href", splt[0] + splt[1]);									
		});
		
		$("div.popupPager a").not(".active").bind("click", function(){
			re_open($(this), img);
			return false;					 
		});
		
		$("div.popupPager a.active").bind("click", function(){
			return false;												
		});
		
		$("div.popupImage").css("cursor", "pointer").bind("click", function(){
			re_open($("div.popupPager a.active").next(), img);
		});
		
		
		if (size['resized']){
			
			$("a.btnexpand").show();
			expand = 0;
			
			$("a.btnexpand").bind("click", function(){
				$("#info").fadeOut(function(){
					$("#main_img").width(imgPreloader.width).height(imgPreloader.height);
					_show(imgPreloader.width + 20, imgPreloader.height + 49);
					$("a.btnexpand").hide();
					$("a.btncontract").show();
				});
				expand = 1;
				return false;
			});
				
			$("a.btncontract").bind("click", function(){
				$("#info").fadeOut(function(){
					$("#main_img").width(size['width']).height(size['height']);
					_show(size['contentWidth'], size['contentHeight']);
					$("a.btnexpand").show();
					$("a.btncontract").hide();
				});
				expand = 0;
				return false;
			});
		}
			
		window.setTimeout( function(){
			_show(size['contentWidth'], size['contentHeight']);
		}, 500);
		$('a.close').bind('click', function(){ _close(img); return false; });
	}
		
	$(document).keyup(function(e){
		switch(e.keyCode){
			case 27:
				_close(img);
				break;
		};
	});
		

	return false;
};

function re_open (el, img){
	
	$("a.btnexpand").hide();
	$("a.btncontract").hide();	
	
	$("a.btnexpand, a.btncontract").unbind("click");
	
	imgPreloader2 = new Image();
	imgPreloader2.src = el.attr("href");
	$("#info").hide();

	var pos = _getCenterPos(100, 100);
	$("div.contentHolder").animate({"top": pos['top'], "left": pos['left'], width: '100px', height: '100px'}, function(){
		$("div.contentHolder").fadeIn();					
	});


	imgPreloader2.onload = function(){
		window.setTimeout(function(){
		size = _fitToViewport(imgPreloader2.width, imgPreloader2.height);
			
		if (!expand && size['resized']){
			$("a.btnexpand").show();
		
			$("a.btnexpand").bind("click", function(){
				$("#info").fadeOut(function(){
					$("#main_img").width(imgPreloader2.width).height(imgPreloader2.height);
					_show(imgPreloader2.width + 20, imgPreloader2.height + 49);
					$("a.btnexpand").hide();
					$("a.btncontract").show();
				});
				expand = 1;
				return false;
			});
				
			$("a.btncontract").bind("click", function(){
				$("#info").fadeOut(function(){
					$("#main_img").width(size['width']).height(size['height']);
					_show(size['contentWidth'], size['contentHeight']);
					$("a.btnexpand").show();
					$("a.btncontract").hide();
				});
				expand = 0;
				return false;
			});
			
			_show(size['contentWidth'], size['contentHeight']);
			$("#main_img").attr("src", el.attr("href")).width(size['width']).height(size['height']);
		}
		else if (size['resized']){
			$("#info").fadeOut(function(){
				$("#main_img").attr("src", el.attr("href")).css({width: imgPreloader2.width, height: imgPreloader2.height});
				_show(imgPreloader2.width + 20, imgPreloader2.height + 49);
				$("a.btnexpand").hide();
				$("a.btncontract").show();
			});
			expand = 1;
			
			$("a.btncontract").bind("click", function(){
				$("#info").fadeOut(function(){
					$("#main_img").width(size['width']).height(size['height']);
					_show(size['contentWidth'], size['contentHeight']);
					$("a.btnexpand").show();
					$("a.btncontract").hide();
				});
				expand = 0;
				return false;
			});
		}
		else {
			$("a.btnexpand").hide();
			$("a.btncontract").hide();
			expand = 1;
			$("#main_img").attr("src", el.attr("href")).css({width: imgPreloader2.width, height: imgPreloader2.height});
			_show(imgPreloader2.width + 20, imgPreloader2.height + 49);			
		}

		$("div.popupPager a.active").removeClass("active");
		el.addClass("active");
		
		$("div.popupPager a").not(".active").bind("click", function(){
			re_open($(this), img);
			return false;
		});
		$("div.popupPager a.active").unbind("click").bind("click", function(){
			return false;												
		});
		
		if ($("div.popupPager a.active").next().length) 
			$("div.popupImage").css("cursor", "pointer").bind("click", function(){
				re_open($("div.popupPager a.active").next(), img);
			});
		else $("div.popupImage").css("cursor", "default").unbind("click");
		
	}, 700);		
	}
}

function _show(width, height){
	
	var pos = _getCenterPos(width, height);

	$('div.contentHolder').animate({
		'width': width,
		'height': height,
		'top': pos['top'],
		'left': pos['left'],
		opacity: 1
	}, "normal", function(){
		$("div.contentHolder div#info").fadeIn("normal");
	});
}

function _build(img){
	var backgroundDiv = "<div class='backgroundDiv'></div>";
	$('body').append(backgroundDiv);
	$('div.backgroundDiv').css('height',$(document).height());
	contentHolder = '<div class="contentHolder"><div id="info"></div></div>';
		
	$('body').append(contentHolder);
	
	$('div.backgroundDiv').css('opacity',0).fadeTo('normal', 0.7, function(){
		var pos = _getCenterPos(100, 100);
		$("div.contentHolder").css({"top": pos['top'], "left": pos['left'], width: '100px', height: '100px', "display": "block"}).fadeIn();																			
	});	
	
	$('div.backgroundDiv').bind('click',function(){
		_close(img);
	})
};
	
function _close(img){
	var offset = img.offset();
	$('div.contentHolder').children().fadeOut(function(){
		$('div.contentHolder').animate({
			left: offset.left,
			top: offset.top,
			width: img.width(),
			height: img.height(),
			opacity: 0
		}, function(){
			$('div.backgroundDiv').fadeOut('normal', function(){
				$('div.backgroundDiv').remove();
				$('div.contentHolder').remove();
			});
		});
	});
};
		
function _getCenterPos(w, h){
	
		var scrollPos = _getScroll();
			
		if($.browser.opera) {
			windowHeight = window.innerHeight;
			windowWidth = window.innerWidth;
		}else{
			windowHeight = $(window).height();
			windowWidth = $(window).width();
		};
		
		if (h > $(document).height()) {
			projectedTop = 120;
			$("div.backgroundDiv").height(h + 240);
		}
		else { 
			projectedTop = (windowHeight/2) + scrollPos['scrollTop'] - h/2;
			if (projectedTop < 0) projectedTop = 120;
		}
			
		projectedLeft = (windowWidth/2) + scrollPos['scrollLeft'] - w/2;
		
		return {top: projectedTop, left: projectedLeft};
};

$(window).resize(function(){ if($(".contentHolder").size() > 0) _reshow() });
$(window).scroll(function(){ if ($("div.contentHolder").height() < $(window).height()) _reshow(); });

function _getScroll(){
	scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
	scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
	return {scrollTop:scrollTop,scrollLeft:scrollLeft};
};


function _fitToViewport(width,height){
	hasBeenResized = false;
		
	contentHeight = height + 49;
	contentWidth = width + 20;
	
	imageWidth = width;
	imageHeight = height;

	if($.browser.opera) {
		windowHeight = window.innerHeight;
		windowWidth = window.innerWidth;
	}else{
		windowHeight = $(window).height();
		windowWidth = $(window).width();
	};
		
	if ((contentWidth > windowWidth) || (contentHeight > windowHeight)) {
		hasBeenResized = true;

		var xscale = (contentWidth + 100) / windowWidth;
		var yscale = (contentHeight + 100) / windowHeight;

		if (yscale>xscale){
			imageWidth = Math.round(width * (1/yscale));
			imageHeight = Math.round(height * (1/yscale));
		} else {
			imageWidth = Math.round(width * (1/xscale));
			imageHeight = Math.round(height * (1/xscale));
		};
		
		contentHeight = imageHeight + 49;
		contentWidth = imageWidth + 20;
	};

	return {
		width:imageWidth,
		height:imageHeight,
		contentHeight:contentHeight,
		contentWidth:contentWidth,
		resized:hasBeenResized
	};
};

function _reshow() {
	
	var pos = _getCenterPos($("div.contentHolder").width(), $("div.contentHolder").height());
	$("div.contentHolder").css("top", pos['top']).css("left", pos['left']);
	
}

function wizard_start(a_link){
	
	reset_form($(".wizard_item:first"));
	
	_build(a_link);
	
	$(".wizard_item").hide();

	var frm = $(".CalcHolder").html();
	
	code = ' <div class="margin" ></div><div class="top_bar"><a href="#" class="close">&nbsp;</a></div><div class="prev"><a href="#" class="pr">&nbsp;</a></div><div class="wizard_show">' + frm + '</div><div class="next"><a href="#" class="nx">&nbsp;</a></div><div class="clear">&nbsp;</div></div>';
	
	$(".CalcHolder form").remove();
	
	$("div.contentHolder #info").html(code);
	
	$("a.close").bind('click', function(){
		__close(a_link, frm);
		return false;
	});
	
	switch(e.keyCode){
		case 27:
			__close(a_link, frm);
			break;
	};

	$('div.backgroundDiv').unbind("click").bind('click',function(){
		__close(a_link, frm);
	});	
	
	$(".wizard_show .wizard_item:first").show().attr("id","act").addClass("fr");
	$(".wizard_show .wizard_item:last").addClass("ls");

	$("a.pr").bind('click',function(){
		
		var h = $(".wizard_item#act").prev().height()+55 + $(".wizard_item#act").prev(".wizard_item").find("div.other_text, .block_depend .check_item").length*22 + $(".wizard_item#act").next(".wizard_item").find("div.other_text input").length*8;
		var pos = _getCenterPos(650, h);
		
		$("#info").fadeOut(function(){
			$("#act").removeAttr("id").hide().prev().fadeIn("normal").attr("id","act");
			$("a.nx").css("display", "block");	
			$("div.prev, div.next").height(h-55);
			$("div.prev a, div.next a").css("margin-top", h/2-35);			
			if($("#act").attr("class").indexOf("fr") != -1)	$("a.pr").hide();
			else $("a.pr").css("display", "block");
			$('div.contentHolder').animate({
				'width': 650,
				'height': h,
				'top': pos['top'],
			} , "normal", function(){	
				$("div.contentHolder div#info").fadeIn("normal");
			});			   
		});

		calc0();
		return false;
	});
	
	$("a.nx").bind('click',function(){
									
		var h = $(".wizard_item#act").next().height()+55 + $(".wizard_item#act").next(".wizard_item").find("div.other_text, .block_depend .check_item").length*22 + $(".wizard_item#act").next(".wizard_item").find("div.other_text input").length*8;
		var pos = _getCenterPos(650, h);
		
		$("#info").fadeOut(function(){
			$("#act").removeAttr("id").hide().next().fadeIn("normal").attr("id","act");
			$("a.pr").css("display", "block");
			$("div.prev, div.next").height(h-55);
			$("div.prev a, div.next a").css("margin-top", h/2-35);			
			if($("#act").attr("class").indexOf("ls") != -1)	$("a.nx").hide();
			else $("a.nx").css("display", "block");
			$('div.contentHolder').animate({
				'width': 650,
				'height': h,
				'top': pos['top'],
			} , "normal", function(){	
				$("div.contentHolder div#info").fadeIn("normal");
			});			   
		});

		calc0();
		return false;
	});
	
	var h = $("#info").height();
	var pos = _getCenterPos(650, h);
	
	
	$("div.prev, div.next").height(h-55);
	$("div.prev a, div.next a").css("margin-top", h/2-35);

	window.setTimeout(function(){
		$('div.contentHolder').animate({
			'width': 650,
			'height': h,
			'top': pos['top'],
			'left':pos['left'],
			'opacity': 1
		} , "normal", function(){	
			$("div.contentHolder div#info").fadeIn("normal");
		});			   
	}, 600);
	
	rebind_form($("div.contentHolder"));
}

function rebind_form(holder){
	
	holder.find("input:checkbox").each(function(){
		$(this).next().bind("mouseover", function(){
			if ($(this).attr("class").indexOf("active_check") == -1) $(this).css("background-image", "url(img/bg_check-hover.gif)");
		}).bind("mouseout", function(){
			if ($(this).attr("class").indexOf("active_check") == -1) $(this).css("background-image", "none");
		}).bind("click", function(){
			if ($(this).prev().attr("checked"))	$(this).css("background-image", "none").attr("style", "").removeClass("active_check");
			else $(this).attr("style", "").css("background-image", "none").addClass("active_check");
		});;
	});
	
	
	holder.find("input:radio").each(function(){
		$(this).next().bind("mouseover", function(){
			if ($(this).attr("class").indexOf("active_rad") == -1) $(this).css("background-image", "url(img/bg_radio-hover.gif)");
		}).bind("mouseout", function(){
			if ($(this).attr("class").indexOf("active_rad") == -1) $(this).css("background-image", "none");
		}).bind("click", function(){
			var name = $(this).prev().attr("name");
			$("div.radio_item label.active_rad").each(function(){
				if ($(this).prev().attr("name") == name) $(this).removeClass("active_rad");
			});
			$(this).attr("style", "").addClass("active_rad");
		});;
	});
	
	holder.find("select").each(function(){
		var sel = $(this);
		var head = sel.parent();
		var cont = head.children().eq(2).width(247);
		var span = head.children().eq(1);

		$(this).next().unbind("click").bind("click", function(){
			cont.toggle("normal");
		});
		$(this).next().next().children().unbind("click").bind("click", function(){
			sel.val($(this).attr("id"));
			span.text($(this).text());
			$(this).parent().hide("normal");
		});
	});
	
	holder.find("input.important").bind("change", function(){
          if ($(this).attr("checked")){
               $(this).parent().next(".block_depend").show();
          }
          else {
               $(this).parent().next(".block_depend").hide().find("input:checkbox").each(function(){
                    $(this).removeAttr("checked");
                    $(this).next("label").removeClass("active_check");
               });
          }
     });
	
	holder.find("input.other").each(function(){
		var text_poll = $(this).parent().next();
		$(this).next().bind("click", function(){
			if ($(this).prev().attr("type") != 'radio') {
				text_poll.toggle();
			}
			else {
				$(this).prev().bind("click", function(){ text_poll.show("normal"); });
				name = $(this).prev().attr("name");
				$("input:radio").not($(this).prev()).each(function(){
					if ($(this).attr("name") == name) $(this).next().bind("click", function(){
						text_poll.hide("normal");
					});
				});
			}
		});
	});
	
	holder.find("div.input-wrap").each(function(){
		$(this).width(50);
		$(this).find("input").width(32).bind("blur + change", function(){
			calc0();															   
		});
	});
	

	
}



function getLeft(element) {
	result = element.offsetLeft;
	if (element.offsetParent) result += getLeft(element.offsetParent);
	return result;
}

function getTop(element) {
	result = element.offsetTop;
	if (element.offsetParent) result += getTop(element.offsetParent);
	return result;
}

function simple_form(){
	reset_form($(".wizard_item:first"));
	$("div.input-wrap").each(function(){
		$(this).width(50);
		$(this).find("input").width(32);
	});
	$(".main .wizard_item").show();
	rebind_form($("div.CalcHolder"));	
}

function __close(elem, frm) {
	var offset = elem.offset();	
	
	$('div.contentHolder').children().fadeOut(function(){
		$('div.contentHolder').animate({
			left: offset.left,
			top: offset.top,
			width: elem.width(),
			height: elem.height(),
			opacity: 0
		});
		$('div.backgroundDiv').fadeOut('normal', function(){
			$('div.backgroundDiv').remove();
			$('div.contentHolder').remove();
	   });	
    });
	$(".CalcHolder").html(frm);
	reset_form($(".wizard_item:first"));	
	rebind_form($("div.CalcHolder"));
}