/**
 * @author Vincent
 * @see http://rafael.adm.br/css_browser_selector/
 * @todo credits
 * @todo fixin class: objects are never destroyed?
 * @todo use live events, to account for new elements
 */

function css_browser_selector(u)
{
	var ua = u.toLowerCase();
	is = function(t){
		return ua.indexOf(t)>-1;
	};
	g='gecko',
	w='webkit',
	s='safari',
	h=document.getElementsByTagName('html')[0],
	b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3')?g+' ff3':is('gecko/')?g:/opera(\s|\/)(\d+)/.test(ua)?'opera opera'+RegExp.$2:is('konqueror')?'konqueror':is('chrome')?w+' chrome':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js'];
	c = b.join(' ');
	h.className += ' '+c;
	return b; //original returned c, not b
}

aBClasses = css_browser_selector(navigator.userAgent);
aBrowserClasses = [];

for(var i = 0, len = aBClasses.length; i<len; i++) {
	var a = aBClasses[i].split(' ');
	for(var j=0, aLen = a.length; j<aLen; j++) {
		aBrowserClasses[a[j]] = true;
	}
}

Fixer = new Class({
	immediateRun : true,
	initialize: function(selector, rules){
        this.selector = selector;
		this.rules = rules;
		if (typeof Fixer.uniqid == 'undefined') Fixer.uniqid = 1;
		if (typeof Fixer.instances == 'undefined') Fixer.instances = [];
		this.uniqid = Fixer.uniqid++;
		Fixer.instances[this.uniqid] = this;
		if (this.immediateRun) this.run();
    },
	run : function() {
		
	},
	globalMethodRef : function (method) {
		return 'Fixer.instances['+this.uniqid+'].'+method+'()';
	},
	callDelayed : function(method,ms) {
		setTimeout(this.globalMethodRef(method),ms);
	},
	destroy : function() {
		delete Fixer.instances[this.uniqid];
	}
});

Fixer.fixers = [];
Fixer.addFixer = function(name,obj) {
	Fixer.fixers[name] = obj;
}
Fixer.runFixer = function(name, selector, rules) {
	if (typeof Fixer.fixers[name] != 'undefined' && typeof selector == 'string' && typeof rules == 'object') {
		new Fixer.fixers[name](selector,rules);
	}
}
Fixer.addFixer('unsupported-selector',new Class({
	Extends: Fixer,
	run: function() {
		this.parent();
		var self = this;
		$(document).ready(function(){
			$(self.selector).css(self.rules);
		});
		this.destroy();
	}
}));
Fixer.addFixer('min-max-height',new Class({
	Extends: Fixer,
	immediateRun : false,
	initialize : function(selector,rules) {
		this.parent(selector,rules);
		var self = this;
		$(document).bind('ready',function(){
			eval(self.globalMethodRef('run'));
		});
		$(window).bind('resize',function(){
			eval(self.globalMethodRef('run'));
		});
	},
	run: function() {
		this.parent();
		var self = this;
		$(this.selector).each(function() {
			var hRule = self.rules['height'];
			if (hRule.charAt(hRule.length-1)=='%') {
				//its a percentage. IE6 could get this wrong. So, fix it.
				var percentage = parseInt(hRule);
				var cbH = $(this.offsetParent).height(); //containing block height
				hRule = ((percentage/100)*cbH)+'px';
			}
			$(this).css('height',hRule); //reset width
			var h = $(this).height();

			var max = (typeof $(this).css('max-height') != 'undefined') ? parseInt($(this).css('max-height')) : 1000000;
			var min = (typeof $(this).css('min-height') != 'undefined') ? parseInt($(this).css('min-height')) : 0;
			
			if (h>max) h = max;
			else if (h<min) h = min;
			
			$(this).height(h);
		});
	}
}));
/**
 * @author Vincent
 * 
 * Browsers: IE 5.5, IE6, IE7, IE8 as IE7, FF 2, FF 3
 * 
 * White-space: pre-line ignores tabs and multiple spaces, 
 * but it breaks off the text at hard returns in the source code, 
 * as well as when it's necessary to prevent the text from breaking out of its box.
 */

var png_selectors = new Array();
var DD_belatedPNG;

function addPngSel(selector)
{
    png_selectors.push(selector);
}

function runThis()
{
    var $els = $('img[src$=png]');

    for(var i in png_selectors) {
	$els.add(png_selectors[i]);
    }

    $els.each(function(){
	DD_belatedPNG.fixPng(this);
    });
}

var isBound = false;

Fixer.addFixer('png',new Class({
	Extends: Fixer,
	run: function() {
		this.parent();
		addPngSel(this.selector);

		if (!isBound) {
		    isBound = true;
		    $(window).ready(function(){
			$.xLazyLoader({
			    js: root_url+'assets/js/DD_belatedPNG/DD_belatedPNG_0.0.8a-min.js',
			    success: function(){
				setTimeout('runThis()',1000);
			    },
			    error: function(){
				//
			    }
			});
		    });
		}
	}
}));
Fixer.addFixer('nth-child',new Class({
	Extends: Fixer,
	run: function() {
		this.parent();
		var self = this;
		$(document).ready(function(){
			$(self.selector).css(self.rules);
		});
		this.destroy();
	}
}));
Fixer.addFixer('doubled-float-margin',new Class({
	Extends: Fixer,
	run: function() {
		this.parent();
		var self = this;
		$(document).ready(function(){
			$(self.selector).css('display','inline');
			$(self.selector).parent().css('zoom','1'); //peekaboo bug
			//see http://www.positioniseverything.net/explorer/doubled-margin.html
		});
		this.destroy();
	}
}));
/**
 * @author Vincent
 */

Fixer.addFixer('pseudo-selector_hover',new Class({
	Extends: Fixer,
	run: function() {
		this.parent();

		var self = this;

		var defaultrules = {};
		for(var rule in self.rules) {
		    defaultrules[rule] = $(self.selector).css(rule);
		}
		
		$(document).ready(function(){
		    $(self.selector).filter(':not(a)').hover(function(){
			$(this).css(self.rules);
		    },function() {
			$(this).css(defaultrules);
		    });
		});
	}
}));



Fixer.addFixer('img-align',new Class({
	Extends: Fixer,
	run: function() {
		this.parent();
		$(document).ready(function() {
			$('img[align=left],img[align=right]').each(function() {
				$(this).css('float',$(this).attr('align'));
			});
		});
	}
}));
if (aBrowserClasses['ie6']) {
	Fixer.runFixer('unsupported-selector','tr td:first-child',{"border-right":"1px solid white"});
	Fixer.runFixer('min-max-height','html,body',{"height":"100%"});
	Fixer.runFixer('png','#menu a',[]);
	Fixer.runFixer('min-max-height','#content',{"left":"275px","top":"2%","position":"absolute","background-color":"#fff25f","height":"88%","padding":"3%"});
	Fixer.runFixer('unsupported-selector','p > img',{"max-width":"100%"});
	Fixer.runFixer('doubled-float-margin','.thumb',[]);
	Fixer.runFixer('min-max-height','img.picture',{"height":"80%","max-height":"600px","display":"block","clear":"both","margin-top":"10px","border":"solid 14px white"});
	Fixer.runFixer('doubled-float-margin','cite',[]);
	Fixer.runFixer('pseudo-selector_hover','#menu a',{"background-image":"url(http:\/\/www.marionteitink.nl\/sh\/assets\/css\/menu_focus.png)"});
	Fixer.runFixer('png','#menu a.current,#menu a:hover',[]);
	Fixer.runFixer('pseudo-selector_hover','#menu #menu_galerie_sub a',{"background":"none"});
	Fixer.runFixer('pseudo-selector_hover','#menu #menu_gastenboek_sub a',{"background":"none"});
	Fixer.runFixer('doubled-float-margin','label',[]);
	Fixer.runFixer('unsupported-selector','label + textarea',{"clear":"both"});
	Fixer.runFixer('unsupported-selector','label + input',{"margin-left":"130px"});
	Fixer.runFixer('unsupported-selector','input[type=submit]',{"margin-top":"1em","font-weight":"700","padding":"2px"});
	Fixer.runFixer('pseudo-selector_hover','input[type=submit]',{"background-color":"#ccc"});
}
if (aBrowserClasses['ie'] || aBrowserClasses['gecko'] || aBrowserClasses['webkit'] || aBrowserClasses['opera']) {
	Fixer.runFixer('nth-child','#content > p:nth-of-type(1)',{"margin-bottom":"20px","font-style":"italic","font-size":"23px","color":"#039"});
}
if (aBrowserClasses['ie6'] || aBrowserClasses['ie7']) {
	Fixer.runFixer('img-align','',[]);
}

