/*
	Functions.js v 0.0.7
	A collection of useful mootools effects.
	ąęś
*/

// -------------------------------------------------Mootools Effects------------------------------------------------//

Element.implement({
	flash: function(to,from,reps,prop,dur) {
		
		//defaults
		if(!reps) { reps = 1; }
		if(!prop) { prop = 'color'; }
		if(!dur) { dur = 250; }
		
		//create effect
		var effect = new Fx.Tween(this, {
				duration: dur,
				link: 'chain'
			})
		
		//do it!
		for(x = 1; x <= reps; x++) {
			effect.start(prop,from,to).start(prop,to,from);
		}
	}
});

/*
Script: SmoothScroll.js
	Class for creating a smooth scrolling effect to all internal links on the page.

License:
	MIT-style license.
*/

var SmoothScroll = new Class({

	Extends: Fx.Scroll,

	initialize: function(options, context){
		context = context || document;
		var doc = context.getDocument(), win = context.getWindow();
		this.parent(doc, options);
		this.links = (this.options.links) ? $$(this.options.links) : $$(doc.links);
		var location = win.location.href.match(/^[^#]*/)[0] + '#';
		this.links.each(function(link){
			if (link.href.indexOf(location) != 0) return;
			var anchor = link.href.substr(location.length);
			if (anchor && $(anchor)) this.useLink(link, anchor);
		}, this);
		if (!Browser.Engine.webkit419) this.addEvent('complete', function(){
			win.location.hash = this.anchor;
		}, true);
	},

	useLink: function(link, anchor){
		link.addEvent('click', function(event){
			this.anchor = anchor;
			this.toElement(anchor);
			event.stop();
		}.bind(this));
	}

});
	
window.addEvent('domready', function() {
	
	//$('strefa_mieszkanca').flash('#ffffff','#72D0FE',500,'color',500);
	new SmoothScroll({ duration:700 }, window); 
	
	var opt = {
		duration: 3000,
		delay: 2000,
		auto:true,
		onMouseEnter: function(){this.stop();},
		onMouseLeave: function(){this.play();}
	}
	var scroller = new QScroller('slogans',opt);
	scroller.load();
	
	new MooTooltips({
		hovered:'.tipper',		// the element that when hovered shows the tip
		ToolTipClass:'ToolTips',	// tooltip display class
		toolTipPosition:-1, // -1 top; 1: bottom - set this as a default position value if none is set on the element
		sticky:false,		// remove tooltip if closed
		fromTop: 0,			// distance from mouse or object
		fromLeft: -55,		// distance from left
		duration: 200,		// fade effect transition duration
		fadeDistance: 20    // the distance the tooltip starts the morph
	});	
	
	$$('.opacity').each(function(el) {	
		op_start = 1; op_end = 0.5;
		el.set('opacity',op_start).addEvents({
			mouseenter: function() {
				this.set('tween', {duration: 200});
				this.tween('opacity',op_end);
			},
			mouseleave: function() {
				this.tween('opacity',op_start);
			}
		});	
	});
});

// ----------------------------------------------------Miscellaneous--------------------------------------------------//
	
function setRegion(regionName) {
	$$('.provinces').each(	function(el) {
								el.selectedIndex = regionName;
								$('qf_province').flash('#cc0000','#ffffff',1,'background-color',500);
								$('ff_province').flash('#cc0000','#ffffff',1,'background-color',500);
							});
}

function showConsultantRegion(id) {
	Mediabox.open('#mb_w'+id, '', '500 150');
}

function showInMediabox(id) {
	Mediabox.open('#mb_w'+id, '', '180 440');
}

function resizeText(el,multiplier) {
	if (el.style.fontSize == "") {
		el.style.fontSize = "1.0em";
	}
	if(multiplier == 0) {
		fontSize = "1.0em";
	} else {
		fontSize = Math.min(1.2, Math.max(0.8, parseFloat(el.style.fontSize) + (multiplier * 0.1))) + "em";
	}

	var morphTextSize = new Fx.Morph(el,{duration:300,transition:Fx.Transitions.linear,'unit':'em'});
	morphTextSize.start({'font-size': parseFloat(fontSize) + (multiplier * 0.05)});	
}

// -------------------------------------------------Class Manipulation------------------------------------------------//

if (typeof FM != 'object') {
    FM = new Object();
}

/**
 * Checks a given class attribute for the presence of a given class
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to remove the class from
 * @param   nameOfClass     The name of the CSS class to check for
 */
FM.checkForClass = function(element, nameOfClass) {
    if (typeof element == 'string') { element = document.getElementById(element); }

    if (element.className == '') {
        return false;
    } else {
        return new RegExp('\\b' + nameOfClass + '\\b').test(element.className);
    }
}

/**
 * Adds a class to an element's class attribute
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to add the class to
 * @param   nameOfClass     Class name to add
 * @see     checkForClass
 */
FM.addClass = function(element, nameOfClass) {
    if (typeof element == 'string') { element = document.getElementById(element); }

    if (!FM.checkForClass(element, nameOfClass)) {
        element.className += (element.className ? ' ' : '') + nameOfClass;
        return true;
    } else {
        return false;
    }
}

/**
 * Removes a class from an element's class attribute
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to remove the class from
 * @param   nameOfClass     Class name to remove
 * @see     checkForClass
 */
FM.removeClass = function(element, nameOfClass) {
    if (typeof element == 'string') { element = document.getElementById(element); }

    if (FM.checkForClass(element, nameOfClass)) {
        element.className = element.className.replace(
            (element.className.indexOf(' ' + nameOfClass) >= 0 ? ' ' + nameOfClass : nameOfClass),
            '');
        return true;
    } else {
        return false;
    }
}

/**
 * Replaces a class with another if the class is present
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to remove the class from
 * @param   class1          Class name to replace
 * @param   class2          Class name to replace it with
 * @see     checkForClass
 * @see     addClass
 * @see     removeClass
 */
FM.replaceClass = function(element, class1, class2) {
    if (typeof element == 'string') { element = document.getElementById(element); }

    if (FM.checkForClass(element, class1)) {
        FM.removeClass(element, class1);
        FM.addClass(element, class2);
        return true;
    } else {
        return false;
    }
}

/**
 * Toggles the specified class on and off
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   element         DOM Element object (or element ID) to toggle the class of
 * @param   nameOfClass     Class name to toggle
 * @see     checkForclass
 * @see     addClass
 * @see     removeClass
 */
FM.toggleClass = function(element, nameOfClass) {
    if (typeof element == 'string') { element = document.getElementById(element); }

    if (FM.checkForClass(element, nameOfClass)) {
        FM.removeClass(element, nameOfClass);
    } else {
        FM.addClass(element, nameOfClass);
    }

    return true;
}
