var hypoMaintenance = false;
var hypoAutomata = true;







jQuery.fn.supersleight = function(settings) {

	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: 'x.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
		afkls;			var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};





;(function($){
 
  if($.browser.msie && document.namespaces["v"] == null) {
    document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
    var ss = document.createStyleSheet().owningElement;
    ss.styleSheet.cssText = "v\\:*{behavior:url(#default#VML);}"
  }
 
  function RR(o) {
    var html = '<div class="ie_border_radius" style="position: absolute; left: 0px; top: 0px; z-index: -1; width:' + (o.width) + "px;height:" + (o.height) + 'px;">'
    html += '<v:roundrect arcsize="' + o.arcSize + '" strokecolor="' + o.strokeColor + '" strokeweight="' + o.strokeWeight + '" style="behavior: url(#default#VML); position:absolute;  antialias: true; width:' + (o.width) + "px;height:" + (o.height) + 'px;' + "" + '" >';
    html += '<v:fill color="' + o.fillColor + '" src="' + o.fillSrc + '" type="tile" style="behavior: url(#default#VML);" />';
    html += '</v:roundrect>';
    html += "</div>"
      
	  return html;
  }
 
  $.fn.borderRadius = !$.browser.msie ? function() {} : function(options){    
 
  	  var options = options || {}
   
      return this.each(function() {
        
        var opts = {}
        
	    	if(this._border_radius_opts) {
    			opts = this._border_radius_opts
    		  $(this).find(".ie_border_radius").remove();
    		}
        else
        {
    			opts.strokeColor = this.currentStyle.borderColor; 
    			opts.strokeWeight = this.currentStyle.borderWidth; 
 
    			opts.fillColor = this.currentStyle.backgroundColor; 
    			opts.fillSrc = this.currentStyle.backgroundImage.replace(/^url\("(.+)"\)$/, '$1'); 
 
    			this.style.border = 'none'; // perhaps add onto padding?
    			this.style.background = 'transparent';
    			this._border_radius_opts = opts
    		}
 
    		opts.width = $(this).outerWidth() 
    		opts.height = $(this).outerHeight() 
    		
    		var r = options.radius || parseInt( this.currentStyle['-ie-border-radius'] ||  this.currentStyle['-moz-border-radius'] || this.currentStyle['moz-border-radius'] );
 
    		opts.arcSize = Math.min( r / Math.min(opts.width, opts.height), 1);
   
        this.innerHTML +=  RR(opts);
        
        if(this.currentStyle.position != "absolute")
          this.style.position = "relative";
          
     	  
        this.style.zoom = 1; // give it a layout 
      });
    }
})(jQuery);





$(document).ready(function() {

	if (hypoMaintenance) hypoMaintenanceMinion();
	
	hypoBrowserDetection();
	
	if (hypoCurrency)
	hypoCurrency.initWithClassName('hypoCurrency');

	$('div#register-form').hide();
	$('a.show-login-form, a.show-register-form').click(function(){
	$('div#register-form, div#login-form').toggle('blind');});

});




var hypoCurrency = {

	conf: {
	
		objectsToInjectHasClass: '',
		eventDataHandler: 'hypoCurrencyValue',
		informationPlateName: 'hypoCurrencyDisplayPlate',
		plateObject: {},
		convertTo: {
		
			'USD': [0.033, "$", ""],
			'HKD': [0.26, "$", ""]
		
		}
	
	},

	initWithClassName: function(initObjectWithClassString) {

		if (typeof initObjectWithClassString != 'string' ) return;
		
		hypoCurrency.conf.objectsToInjectHasClass = initObjectWithClassString;

		hypoCurrency.behavior.makePlate();
	
		var arrayObjectsToInject = $('body').find('.' + hypoCurrency.conf.objectsToInjectHasClass);
		
		if (arrayObjectsToInject.length == 0) return;

		$.each(arrayObjectsToInject, function(index, children) {
			
			objectToInject = $(children);
			
			stringToParse = String(objectToInject.text());
			arrayResults = stringToParse.match(/\d+[,\d+]+/);
			
			if (arrayResults == null || arrayResults.length == 0) return true;
			
			objectToInject.data("hypoCurrencyValue", Number(String(arrayResults[0]).replace(",", "")));
			
			objectToInject.bind('mouseenter', hypoCurrency.behavior.mouseEnter);
			
			objectToInject.bind('mouseleave', hypoCurrency.behavior.mouseLeave);
			
		});
		
		
		
	},
	
	behavior: {
	
		mouseEnter: function(event) {
		
			var objectEventTarget = $(event.target);
			hypoCurrency.behavior.createPlateOnElement(objectEventTarget);
			
		},
		
		mouseLeave: function() {
			
			hypoCurrency.behavior.hidePlate();
			
		},
		
		makePlate: function() {
		
			if ($('body').find("div#" + hypoCurrency.conf.informationPlateName).length != 0) return;
			
			var plateObject = $("<div id='" + hypoCurrency.conf.informationPlateName + "'></div>");
			
			hypoCurrency.conf.plateObject = plateObject;

			hypoCurrency.conf.plateObject.fadeTo(1, 0).appendTo('body');
			
		},
		
		createPlateOnElement: function(targetElement) {
			
			targetElement = targetElement.closest('.' + hypoCurrency.conf.objectsToInjectHasClass);
			
			hypoCurrency.conf.plateObject.html("…");
			
			if (targetElement.data(String(hypoCurrency.conf.eventDataHandler)) == undefined) return;
						
			
			hypoCurrency.behavior.populatePlateWithOriginalValue(targetElement.data(hypoCurrency.conf.eventDataHandler));
			
			var leftAnchor = targetElement.offset().left - Number(String(targetElement.css("paddingRight")).match(/\d+/)[0]);
			
			var topAnchor = targetElement.offset().top - hypoCurrency.conf.plateObject.height() - 27;

			hypoCurrency.conf.plateObject
				.stop(true, true)
				.css("visibility", "visible")
				.css("top", topAnchor + 9)
				.css("left", leftAnchor)
				.animate({
				
					top: topAnchor,
					opacity: 1
				
				}, 100);
						
		},
		
		populatePlateWithOriginalValue: function(originalValueToBeConverted) {

			if (typeof originalValueToBeConverted == 'undefined') return;
		
			var stringContext = "";
			
			$.each(hypoCurrency.conf.convertTo, function(index, child) {
			
				var currencyName = String(index);
				var currencyRatio = child[0];
				var currencyPrefix = child[1];
				var currencySuffix = child[2];
				
				stringContext += "<small>≈ ";
				stringContext += currencyName + "</small> ";
				stringContext += currencyPrefix + Math.ceil(originalValueToBeConverted * currencyRatio) + currencySuffix;
				stringContext += "<br />";
				
			});
			
			hypoCurrency.conf.plateObject.html(stringContext);
			
		},
		
		hidePlate: function() {
		
			hypoCurrency.conf.plateObject
				.fadeTo(100, 0)
				.css("visibility", "hidden");
		
		}
	
	}

}





function hypoBrowserDetection(){

	function hypoWin(){	$('span#switch').animate({ left: 30 }, 300); $('body').addClass('show-win');}
	function hypoMac(){	$('span#switch').animate({ left: 1 }, 300); $('body').removeClass('show-win');}

	pageLang == pageLang || 'zh';

	if (pageLang == 'en'){

		$('body').addClass('show-win');
		$('#switch-bg').remove();
		$('span#switch').remove();

	} else if(!$.browser.mac) {
	
		$('body').addClass('show-win');
		$('span#switch').toggle(hypoMac, hypoWin);
	
	} else {

		$('span#switch').toggle(hypoWin, hypoMac);
		
	}

}





function hypoPluginNotification() {
	
	$('a.hypoPluginGet').bind("click", function(){
		$('div#darkside,div#downloading').show();
	});

	$('a.hypoPluginDismiss').bind("click", function(){
		$('div#downloading, div#darkside').hide();
	});

}







function hypoCyclePictures() {

	$('.cycle-pics').cycle({fx:'fade',speed:800,timeout:5000,pause:1});
	
}





function hypoTutorial(){

	$("#tutorials").load("tutorials/" + hypoProduct + "-1.html");
	$('p#load-tutorials a').each(function(e){$(this).attr("rel",e+1);});

	$('p#load-tutorials a').click(function(){
	
		tutorialNumber = $(this).attr("rel");
		$("#tutorials").html("").load("tutorials/" + hypoProduct + "-" + tutorialNumber + ".html");
		
	});

}





function hypoMaintenanceMinion() {

	$('div#header').before('<div id="warning" style="background:url(http://hypo.cc/img/bg/stripe.png); width:100%; height:30px; text-align:center; border-bottom:#333 solid 1px;"><span style="line-height:30px; color:white;">網站進行調整中，但不影響產品製作 / We’re pushing pixels around.</span></div>');
	
	$('div#warning').click(function(){$(this).slideUp();});

}





function hypoSetTitleWithName (aName) {

	if (window && window.console && window.console.error)
	window.console.error("hypoSetTitleWithName is deprecated.  Use the title template variable instead.");

	if (!aName) return;
	document.title = (aName + " — " + document.title);
	
}
