// @version $Id: extensions.js 4647 2011-09-28 15:16:03Z sjelner $

//this file contains all the jquery extensions

$.extend({
	/*
	 * this method hardcodes values into functions. because sometimes in loops,
	 * if you use functions for later events, you run into massive scoping
	 * problems. until now this only works for int and string
	 * (no floats, arrays, objects, functions and so on)
	 */
	hardcode: function(func,args) {
		if(typeof(func) === 'function' && typeof(args) === 'object') {
			var newFunc;
			var body = 'newFunc=function(args){func(args,[';
			$.each(args,function(index,val) {
				if(typeof(val) === 'string') val = "'"+val.replace("'","\\'")+"'";
				body += val+',';
			});
			if(body.substr(body.length-1,1) === ',') body = body.substring(0,body.length-1);
			body += ']);}';
			eval(body);
			return newFunc;
		} else return false;
	},

	/*
	 * this method takes an associative array and transforms it into real events.
	 *
	 * 1. level: 'bind' or 'live'
	 * 2. level: selectors, 'window' or 'document'
	 * 3. level: event types
	 */
	behaviour: function(evts) {
		$.each(evts,function(key,val) {
			$.each(val,function(key2,val2) {
				var els;
				if(key2 === 'window' || key2 === 'document') eval('els = $('+key2+');');
				else els = $(key2);
				$.each(val2,function(key3,val3) { eval('els.'+key+'(key3,val3);'); });
			});
		});

		return true;
	},

	/*
	 * this method savely checks, whether an image is loaded
	 * it also works for images, that are already in the cache
	 * or that IE marks as "uninitialized"
	 */
	imgLoaded: function(img,callback) {
		img = $(img);
		if(
			img[0].complete ||
			img[0].readyState === 4 ||
			(
				typeof(img[0].height) !== 'undefined' &&
				img[0].height !== 0
			)
		) { callback(); }
		/*
		 * we have to directly unbind the load-event, otherwise IE6,7,8 fire for every src-change
		 * which results in unwanted behaviour (recursive firing)
		 */
		else { img.load( function() { img.unbind('load'); callback(); }); }
	},

	decodeUmlaute: function(val) {
		if(typeof(val) === 'string') {
			var a = ['&Auml;','&auml;','&Ouml;','&ouml;','&Uuml;','&uuml;','&szlig;'];
			var b = ['\u00c4','\u00e4','\u00d6','\u00f6','\u00dc','\u00fc','\u00df'];
			for(var i=0;i<a.length;i++) eval('val = val.replace(/'+a[i]+'/g,b[i]);');
		};
		return val;
	}
});

/*
 * We need to delete the whole opacity-inlien-style completely. this
 * can only be done the very dirty way!
 */
$.fn.removeInlineStyle = function(prop) {
	if(typeof(prop) !== 'object' || typeof(prop.length) === 'undefined') prop = [prop];
	$(this).each(function(i,el) {
		el = $(el);
		for(var j=0;j<prop.length;j++) {
			eval('el.attr("style",el.attr("style").replace(/'+prop[j]+'[^;]+;?/i,""));');
		};
	});
	return this;
};

//make a popup out of any link
$.fn.bindPopup = function() {
	$(this).each(function(i,el) {
		el = $(el);
		el.removeAttr('data-event'); //prevent recursive transformation
		var tmp1 = el.attr('data-config');
		if(typeof(tmp1) !== 'undefined' && tmp1 !== '') {
			try { eval('tmp1 = '+tmp1+';'); } catch(ex) {};
		};
		el.removeAttr('data-config');
		var href = el.attr('href');
		if(typeof(href) === 'string' && href !== '' && href !== '#') {
			el.bind({
				'click': function(e) {
					e.preventDefault();
					$.ajax(href,{
						'type': 'GET',
						'data': 'nocache='+config.nocache, //no caching please
						'dataType': 'text',
						'success': function(data) {
                                                        window.popupHref = href;
							var tmp2 = {
								top: 90,
								hamlCallback: function() { return data; },
								beforeMeasure: function(popup) {
									var sel = '#popup-content'+popup.stack+' .popup-content-wrapper ';
									cufonSets.main(sel);
									cufonSets.article(sel);
									readyTransforms(sel);
								},
								beforeOpen: function() {
									$(window).scrollTop(0).scrollLeft(0);
								}
							};
							if(typeof(tmp1) === 'object') $.extend(tmp2,tmp1);
							new popup(tmp2);
						}
					});
				}
			});
		};
	});
};

//transform an element to a popup
$.fn.openPopup = function() {
	$(this).each(function(i,el) {
		el = $(el);
		el.removeAttr('data-transform'); //prevent recursive transformation
		new popup({
			top: 90,
			hamlCallback: function() { return [el]; },
			beforeMeasure: function(popup) {
				el.removeClass('invisible'); //make element visible
				var sel = '#popup-content'+popup.stack+' .popup-content-wrapper ';
				cufonSets.main(sel);
				cufonSets.article(sel);
				readyTransforms(sel);
			},
			beforeOpen: function() {
				$(window).scrollTop(0).scrollLeft(0);
			}
		});
	});
};

//return src for a CSS background-image
$.fn.getBGSrc = function() {
	var src = false;
	if($(this).css('background-image').match(/^url\(["']?([^"']*?)["']?\)$/)) src = RegExp.$1;
	return src;
};
