//Page init scripts
jQuery(function(){
	clearInputs();
	initFadeBG();
	initOpenClose();
	initCrowCol();
	initSmoothScroll();
});
//Smooth scroll page
function initSmoothScroll(){
	jQuery('a.smooth-scroll').bind('click', function(){
		var that = jQuery(this);
		var thatHref = that.attr('href');
		jQuery('html, body').animate({ scrollTop: jQuery(thatHref).offset().top }, { queue: false, duration: 500 });
		return false;
	});
};
//Fade hover animattion
function initFadeBG(){
	jQuery('.promo .block').fadeHover({
		speedAnimation: 500
	});
};
jQuery.fn.fadeHover = function(_options){
	var _options = jQuery.extend({
		speedAnimation: 350,
		ieFix: true
	},_options);

	return this.each(function(){
		var set = jQuery(this);
		var speedAnimation = _options.speedAnimation;
		if(!set.length) return;
		var fakaBG = jQuery('<div class="fade-background-block"></div>');
		fakaBG.css({
			position: 'absolute',
			width: set.outerWidth(),
			height: set.outerHeight(),
			top: 0,
			left: 0,
			zIndex: 1,
			background: set.css('background-image') + ' ' + set.css('background-position') + ' ' + set.css('background-repeat')
		});
		set.append(fakaBG);
		if(jQuery.browser.msie && _options.ieFix) { fakaBG.hide(); }
		else { fakaBG.css({ opacity: 0 }); };
		set.bind('mouseenter', function(){
			if(jQuery.browser.msie && _options.ieFix) {
				fakaBG.show();
			}
			else {
				fakaBG.animate({ opacity: 1 }, { queue: false, duration: speedAnimation });
			};
		}).bind('mouseleave', function(){
			if(jQuery.browser.msie && _options.ieFix) {
				fakaBG.hide();
			}
			else {
				fakaBG.animate({ opacity: 0 }, { queue: false, duration: speedAnimation });
			};
		})
		set.children().not(fakaBG).css({ position: 'relative', zIndex: 5 });
		set.css({ background: 'none' });
	});
};
// OpenClose init
function initOpenClose(){
	jQuery('.subnav > li').OpenClose({
		activeClass:'active',
		opener:'>a',
		slider:'div.drop',
		effect:'slide',
		outClickHide: true,
		animSpeed:500
	});
};
// open-close plugin
jQuery.fn.OpenClose = function(_options){
	// default options
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'.slide',
		animSpeed: 400,
		animStart:false,
		animEnd:false,
		effect:'fade',
		outClickHide: false,
		event:'click'
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _slideSpeed = _options.animSpeed;
		var _activeClass = _options.activeClass;
		var _opener = jQuery(_options.opener, _holder);
		var _slider = jQuery(_options.slider, _holder);
		var _animStart = _options.animStart;
		var _animEnd = _options.animEnd;
		var _effect = _options.effect;
		var _event = _options.event;
		if(_slider.length) {
			_opener.bind(_event,function(){
				if(!_slider.is(':animated')) {
					if(typeof _animStart === 'function') _animStart();
					if(_holder.hasClass(_activeClass)) {
						_slider[_effect=='fade' ? 'fadeOut' : 'slideUp'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
						_holder.removeClass(_activeClass);
					} else {
						_holder.addClass(_activeClass);
						_slider[_effect=='fade' ? 'fadeIn' : 'slideDown'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
					}
				}
				return false;
			});
			if(_holder.hasClass(_activeClass)) _slider.show();
			else _slider.hide();
			if(_options.outClickHide){
				jQuery('body').bind('click', function(e){
					if(_holder.hasClass(_activeClass)) {
						if(!jQuery(e.target).parents().is(_holder)){
							_slider[_effect=='fade' ? 'fadeOut' : 'slideUp'](_slideSpeed,function(){
								if(typeof _animEnd === 'function') _animEnd();
							});
							_holder.removeClass(_activeClass);
						}
					}
				});
			};
		}
	});
};
//Clear input text
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
};
function initCrowCol(){
	jQuery('.scrollable-wrap').crawlBox({
		crawElement:'.scrollable',
		textElement:'.box'
	});
}
/*******************************************************************************************/
// jquery.event.wheel.js - rev 1
// Copyright (c) 2008, Three Dub Media (http://threedubmedia.com)
// Liscensed under the MIT License (MIT-LICENSE.txt)
// http://www.opensource.org/licenses/mit-license.php
// Created: 2008-07-01 | Updated: 2008-07-14
// jQuery(body).bind('wheel',function(event,delta){    alert( delta>0 ? "up" : "down" );    });
/*******************************************************************************************/
;(function($){$.fn.wheel=function(a){return this[a?"bind":"trigger"]("wheel",a)};$.event.special.wheel={setup:function(){$.event.add(this,b,wheelHandler,{})},teardown:function(){$.event.remove(this,b,wheelHandler)}};var b=!$.browser.mozilla?"mousewheel":"DOMMouseScroll"+($.browser.version<"1.9"?" mousemove":"");function wheelHandler(a){switch(a.type){case"mousemove":return $.extend(a.data,{clientX:a.clientX,clientY:a.clientY,pageX:a.pageX,pageY:a.pageY});case"DOMMouseScroll":$.extend(a,a.data);a.delta=-a.detail/3;break;case"mousewheel":a.delta=a.wheelDelta/120;if($.browser.opera)a.delta*=-1;break}a.type="wheel";return $.event.handle.call(this,a,a.delta)}})(jQuery);

jQuery.fn.crawlBox = function(_options){
	// defaults options
	var _options = jQuery.extend({
		speed:1,
		crawElement:'div',
		textElement:'p',
		hoverClass:'viewText'
	},_options);
	
	return this.each(function(){
		var set = jQuery(this);
		var slideList = jQuery(_options.crawElement, set).css('position','relative');
		var slideItem = jQuery(_options.textElement, set);
		var koef = 1;
		
		// set parametrs *******************************************************
		var summHeight = 0;
		slideItem.each(function(){
			summHeight += jQuery(this).outerHeight(true);
		});
		var _duration = summHeight*50 / _options.speed;
		slideList.append(slideItem.clone()).append(slideItem.clone());
		slideList.css({ height: summHeight*3 , top: -summHeight });
		var animate = function() {
			slideList.animate({top: -summHeight*2 }, {queue:false, duration:_duration*koef, easing:'linear', complete:function(){
				slideList.css({ top: -summHeight });
				koef = 1;
				animate();
			}});
		}
		animate();
		set.bind('mouseenter', function(){
			slideList.stop();
			set.addClass(_options.hoverClass);
		}).bind('mouseleave', function(){
			set.removeClass(_options.hoverClass);
			koef = (summHeight*2 + parseInt(slideList.css('top')))/summHeight;
			animate();
		});
		set.bind('wheel',function(event,delta){
			var _marginScroll;
			var step = parseInt(slideList.css('top')) + 20*delta;
			
			if (delta < 0 ) {
				if( step < -summHeight*2) _marginScroll = -summHeight
				else _marginScroll = step + 20*delta
			}
			else {
				if( step > -summHeight) _marginScroll = -summHeight*2
				else _marginScroll = step + 20*delta
			};
			slideList.animate({ top:_marginScroll}, {queue:false, duration:10, easing:'linear', complete:function(){
				koef = (summHeight*2 + parseInt(slideList.css('top')))/summHeight;
			}});
			return false;
		});
	});
};
// mobile browsers detect
browserPlatform = {
	platforms: [
		{
			// Blackberry <5
			uaString:['BlackBerry','midp'],
			cssFile:'blackberry.css'
		},
		{
			// Symbian phones
			uaString:['symbian','midp'],
			cssFile:'symbian.css'
		},
		{
			// Opera Mobile
			uaString:['opera','mobi'],
			cssFile:'opera.css'
		},
		{
			// IE Mobile <6
			uaString:['msie','ppc'],
			cssFile:'ieppc.css'
		},
		{
			// IE Mobile 6+
			uaString:'iemobile',
			cssFile:'iemobile.css'
		},
		{
			// Palm WebOS
			uaString:'webos',
			cssFile:'webos.css'
		},
		{
			// Android
			uaString:'Android',
			cssFile:'android.css'
		},
		{
			// Blackberry 6+
			uaString:['BlackBerry','6.0','mobi'],
			cssFile:'blackberry6.0.css'
		},
		{
			// iPad
			uaString:'ipad',
			cssFile:'ipad.css',
			miscHead:''
		},
		{
			// iPhone and other webkit browsers
			uaString:['safari','mobi'],
			cssFile:'safari.css',
			miscHead:''
		}
	],
	options: {
		cssPath:'css/',
		mobileCSS:'allmobile.css'
	},
	init:function(){
		this.checkMobile();
		this.parsePlatforms();
		return this;
	},
	checkMobile: function() {
		if(this.uaMatch('mobi') || this.uaMatch('midp') || this.uaMatch('ppc') || this.uaMatch('webos')) {
			this.attachStyles({cssFile:this.options.mobileCSS});
		}
	},
	parsePlatforms: function() {
		for(var i = 0; i < this.platforms.length; i++) {
			if(typeof this.platforms[i].uaString === 'string') {
				if(this.uaMatch(this.platforms[i].uaString)) {
					this.attachStyles(this.platforms[i]);
					break;
				}
			} else {
				for(var j = 0, allMatch = true; j < this.platforms[i].uaString.length; j++) {
					if(!this.uaMatch(this.platforms[i].uaString[j])) {
						allMatch = false;
					}
				}
				if(allMatch) {
					this.attachStyles(this.platforms[i]);
					break;
				}
			}
		}
	},
	attachStyles: function(platform) {
		if(platform.cssFile) {
			document.write('<link rel="stylesheet" href="' + this.options.cssPath + platform.cssFile + '" type="text/css"/>');
		}
		if(platform.miscHead) {
			document.write(platform.miscHead);
		}
	},
	uaMatch:function(str) {
		if(!this.ua) {
			this.ua = navigator.userAgent.toLowerCase();
		}
		return this.ua.indexOf(str.toLowerCase()) != -1;
	}
}.init();
