/*******************************************************************
**--> Intro: For Spark Customize WordPress Themes scripts
**--> Author: atozwordpress.com@gmail.com
**--> WebSite: http://www.atozwordpress.com
*******************************************************************/


jQuery.fn.extend({
	//autoResize
	//相对父元素宽高重置该元素大小.保持该元素比例,但可能盖住一部分,需要确定父元素的宽高;
	autoResize:function(elem){
		var mw=this.parent().width();
		var mh=this.parent().height();
		if(elem!='' && elem!=null){
			mw=jQuery(elem).width();
			mh=jQuery(elem).height();
		}
		if(!mw || !mh){
			return;
		}
		var tw=this.width();
		var th=this.height();
		if(tw/th<mw/mh){
			this.css({
				width: mw,
				height: mw/tw*th
			});
		}else{
			this.css({
				height: mh,
				width: mh/th*tw
			});
		}
	},
	//elemResize
	//重置元素大小;
	//第一参数为要重置的宽;
	//第二参数为要重置的高;
	//一二参数至少需填一条;
	elemResize:function(width,height){
		if(width!='' && width!=0){
			var mw=width-0;
		}
		if(height!='' && height!=0){
			var mh=height-0;
		}
		var tw=this.width();
		var th=this.height();
		if(mw && !mh){
			this.css({
				width:mw,
				height:mw/tw*th
			});
		};
		if(!mw && mh){
			this.css({
				height:mh,
				width:mh/th*tw
			});
		}
		if(mw && mh){
			if(tw/th<mw/mh){
				this.css({
					width: mw,
					height: mw/tw*th
				});
			}else{
				this.css({
					height: mh,
					width: mh/th*tw
				});
			}
		}
	}
});


jQuery(document).ready(function(){
	//设定Class为autoResize的元素执行autoResize;
	jQuery('.autoResize').autoResize();
	//设定Class带resize_开头的元素执行resize;
	jQuery("[class*='resize_']").each(function(){
		var cn=jQuery(this).attr('class');
		var index=cn.indexOf('resize_');
		cn=cn.substring(index);
		index=cn.match(/_/g);
		if(index.length>2){return;}
		cn=cn.split('_');
		var w=cn[1];
		var h=cn[2];
		jQuery(this).elemResize(w,h);
	});
});

//去除数组重复值;
function unique( array ) {
		//    <summary>
		//        Removes all duplicate elements from an array of elements.
		//    </summary>
		//    <param name="array" type="Array<Element>">The array to translate</param>
		//    <returns type="Array<Element>">The array after translation.</returns>
		var ret = [], done = {};
		try {
				for ( var i = 0, length = array.length; i < length; i++ ) {
						//var id = jQuery.data( array[ i ] );
						var id = array[ i ];
						if ( !done[ id ] ) {
								done[ id ] = true;
								ret.push( array[ i ] );
						}
				}
		} catch( e ) {
				ret = array;
		}
		return ret;
}

/*
 * runOnLoad.js: portable registration for onload event handlers.
 * 
 * This module defines a single runOnLoad() function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad() will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used.  Functions registered with runOnLoad()
 * will be invoked in the order in which they were registered.  There is no
 * way to deregister a function once it has been passed to runOnLoad().
 *
 * In old browsers that do not support addEventListener() or attachEvent(),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}
 
runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.
 
// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing
 
    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }
 
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};
 
// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;

