﻿//Fade Function
(function(jQuery) {
	jQuery.fn.customFadeIn = function(speed, callback) {
		jQuery(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				jQuery(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	jQuery.fn.customFadeOut = function(speed, callback) {
		jQuery(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				jQuery(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

jQuery(document).ready(function(){	
	//Product Modal Window
	jQuery('a[name="modal"]').click(function(e) {
		var href = jQuery(this).attr('href');
		var rel = "." + jQuery(this).attr('rel');
		var content = rel + " .window_body";
		var loader = rel + " .loading";
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
		
		jQuery(content).html('');
		
		jQuery(rel).css("top", jQuery(window).scrollTop()+40 + "px");
		jQuery(rel).css("left", winW/2-jQuery(rel).width()/2);
		jQuery(rel).customFadeIn(1500);	
		
		jQuery(content).hide("fast",loadContent);

		function loadContent() {
			jQuery(content).load(href,'',showNewContent())
		}
		function showNewContent() {
			jQuery(content).show("normal");
		}
		return false;
		
	});
	jQuery('a.close_popup').click(function(e) {										   
		jQuery('.window_small, .window_large, .window_signup, #popup_bg').customFadeOut('fast', function() {});
		return false;
	});	
	jQuery(document).keypress(function(e){
		if(e.keyCode==27){
			jQuery('.window_small, .window_large, .window_signup, #popup_bg').customFadeOut('fast', function() {});
		}
	});
	
	
	//Signup Modal Window
	jQuery('a[name="signup"]').click(function(e) {
		var id = jQuery(this).attr('href');
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
		jQuery('#popup_bg').css({'width':maskWidth,'height':maskHeight});	
		jQuery('#popup_bg').customFadeIn(1000);
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
		jQuery(id).css("top", jQuery(window).scrollTop()+40 + "px");
		jQuery(id).css('left', winW/2-jQuery(id).width()/2);
		jQuery(id).customFadeIn(1500);
		return false;
	});	
	
	//Smooth Scroll
	jQuery('a[rel="scroll"]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
			&& location.hostname == this.hostname) {
			var jQuerytarget = jQuery(this.hash);
			jQuerytarget = jQuerytarget.length && jQuerytarget
			|| jQuery('[name=' + this.hash.slice(1) +']');
			if (jQuerytarget.length) {
				var targetOffset = jQuerytarget.offset().top;
				jQuery('html,body')
				.animate({scrollTop: targetOffset}, 1500);
				return false;
			}
		}
	});
	
	//External Link
	jQuery('a[rel="external"]').click(function(){
        window.open( jQuery(this).attr('href') );
        return false;
    });

});
