/***************************
**** CREATE: 19.05.2011 ****
****** VERSION: 1.0.3 ******
****** www.media5.com ******
***************************/

(function($, undefined){
	
	(function(browser){
		$.each($.browser, function(i, val) {if(!browser) browser = i;});
		$('html').addClass('js').addClass(browser).addClass(browser+''+$.browser.version.replace(/\./g,'_'));
	})('');
	
	
	
	$.ex = {
		overlay : function(options,func){
			switch(options)
			{
				case 'hide':
					this.widget('overlay').hide(); 
					return false;
				break;
				case 'close':
					this.widget('overlay').mousedown(function(){
						if(typeof func == 'function') func();
					});
					return false;
				break;
			}
			var height = $(window).height() > $('body').outerHeight() ? $(window).height() : $('body').outerHeight();
			var options2 = $.extend({width:$(window).width(),height:height,background:'#000',zIndex:1000,opacity:0.9,wrap:false},options);
			$('html').animate({scrollTop:0},300);
			return this.widget('overlay',options2).show();
		},
		middle : function(id){
			id = $(id);
			var wh = {width:$(window).width(),height:$(window).height(),scrollTop:$('html').scrollTop()};			
			id.css({zIndex:2000,position:'absolute',left:'50%', top:'50%',marginLeft:-id.outerWidth()/2,marginTop:-id.outerHeight()/2+wh.scrollTop}).show();
		},
		wrap : function(){
			var idBody = $('body');
			var idHtml = $('html');
			var wrap = idBody.is('.exWrap');
			var bodyWidth = idBody.width();
			var scrollTop = idHtml.scrollTop();
			if(wrap)
			{
				idHtml.css({overflow:'auto'}).scrollTop(scrollTop);
				idBody.width('auto').removeClass('exWrap');
			} else {
				idHtml.css({overflow:'hidden'}).scrollTop(scrollTop);
				idBody.width(bodyWidth).addClass('exWrap');
			}
			return this;
		},widget : function(type, css){
			switch(type){
				case 'overlay':
					var obj = $('#exOverlay');
					if(!obj.length) {
						$('<div id="exOverlay" style="position:absolute; left:0; top:0; display:none; padding-bottom:120px"></div>').appendTo('body');
						if($.browser.msie && $.browser.version == '7.0') {$('#exOverlay').css('top','50px');}
							//
						obj = $('#exOverlay');
					}
					/*var docheight = $('.container').css('height');
					alert(docheight);*/
					/*if ($(document).height < 1000) {
						$("#exOverlay").css('padding-bottom', '125px');
					}*/
					return obj.css((css || {}));
				break;
				default : return true;
			}
		}
	};
	
	$.fn.slider = function(type,func,span){
		switch(type){
			case "slideNews":
				news_block = $('div.news-block');
				news_slide = $('div.news_slide');
				var height = news_block.filter(':visible').height();
				var limit = 166;
				if($.browser.safari || $.browser.chrome) limit = 159;
				height = height < limit ? height : limit;
				news_slide.css({height:height});
			break;
			case "slideimages":
				shape_image = $('img.shape_image');
				
				function shape_image_visible_index(){
					return shape_image.index(shape_image.filter(':visible'));
				}
						
				var shape_size = shape_image.size();
				
				$('a.shape_next').click(function(){
					var index = shape_image_visible_index()+1;
					if(index < shape_size)
						span.eq(index).trigger('click');
					else
						span.eq(0).trigger('click');
					return false;
				});
				$('a.shape_prev').click(function(){
					var index = shape_image_visible_index()-1;
					if(index >= 0)
						span.eq(index).trigger('click');
					else
						span.eq(shape_size-1).trigger('click');
					return false;
				});
			break;
		}
		
		this.each(function(){
			span = $(this).children('li').children('span');
			span.click(function(e){
				e.stopPropagation();
				if( type == 'slideimages') if(shape_image.is(':animated')) return false;
				
				var active = span.index(span.filter('.active').removeClass('active'));
				var index = span.index($(this).addClass('active'));
				
				if(index == active) return false;
				typeof func == 'function' ? func(index, index > active ? 'next' : 'prev'): true ;
			});
		});
		return this;
	}

	$.fn.menuToggle = function(){
		menu_block = $('#menu_block');
		//if($.browser.msie && $.browser.version == '6.0')
			var zindexie6 = $('#zindexie6').find('a.link, .crumbs');


		this.each(function(){
			var close_button = $('#menu_button_close');
			var menu_button = $(this);
			menu_button.click(function(){
				if(menu_button.is('.menu_button_open')) return false;
				menu_button.addClass('menu_button_open');
				
				var left = $(this).position().left+$(this).width();
				
				close_button.show().css({left:left});
				menu_block.show();
				correctContentHeight();
				
				//if($.browser.msie && $.browser.version == '6.0')
					zindexie6.hide();
					$('#zindexie6').css({zIndex:1001});
				
				return false;
			});
			close_button.click(function(){
				menu_button.removeClass('menu_button_open');
				close_button.hide();
				menu_block.hide();
				correctContentHeight('close');
				
				//if($.browser.msie && $.browser.version == '6.0')
					zindexie6.show();
					$('#zindexie6').css({zIndex:0});
					
				return false;
			});
		});
	}
	
	$.fn.menuTree = function(){
		function toggle(li){
			var className = li.children('ul').length ? li.attr('class') : '';
			switch(className){
				case 'li_close':
					li.removeClass('li_close').addClass('li_open');
					correctContentHeight();
					return false;
				break;
				case 'li_open':
					li.removeClass('li_open').addClass('li_close');
					correctContentHeight();
					return false;
				break;
				default:
					li.removeClass('li_open').removeClass('li_close');
					correctContentHeight();
			}
		}

		this.each(function(){			
			$(this).children('li').click(function(event){
				if(event.target.nodeName === 'LI')
				{
					var li = $(this);
					return toggle(li);
				}
			});
		});
		
	}
	
	$.fn.sliderCenter = function(){
		this.each(function(){
			$(this).css({
				position:'absolute',
				left:'50%'
			}).css({
				marginLeft: -$(this).width()/2
			});
		});
		return this;	
	}
	
	$.fn.switches = function(options){
		var a = this;
		return this.each(function(){
			$(this).click(function(){
					
				switch(options.type){
					case "radio":
						//if($(this).is('.switches_select')) return false;
						a.filter('.switches_select').removeClass('switches_select');
						$(this).addClass('switches_select');
					break;
					case "checkbox":
						if(!options.toggle && a.filter('.switches_select').length == 1 && $(this).is('.switches_select')) return false;
						$(this).toggleClass('switches_select');
					break;
				}
				
				

				if(typeof options.onChange == 'function') options.onChange($(this));
				
				return false;
			});
		});
	}
	
	
	
	$.combobox = function(event, action, title, result){
		var e = $(event);
		//$.ex['wrap']();

		switch(action){
			case 'onChange':
				if(result) window['comboboxSelectFake'].next().next(result).text(e.text());
				window['comboboxSelectFake']/*.unbind('mouseleave')*/.hide();
				window['comboboxSelectClose'].hide();
				e.parents('.ex-select').find('a').filter('.this-selected').removeClass('this-selected');
				e.addClass('this-selected');
				window['comboboxSelected'].removeClass('ex-select-visible').children('span').children('span').text(title);
				window['comboboxSelect'].val(e.data('value')).trigger('change');
			break;
			case 'Close':
				window['comboboxSelectFake'].hide();
				window['comboboxSelectClose'].hide();
				window['comboboxSelected'].removeClass('ex-select-visible');
			break;
			default:
				var offs = e.offset();
				var offs_width = e.outerWidth();
				var offs_height = e.outerHeight();
				if(window['comboboxSelectFake']) window['comboboxSelectFake'].hide();
				window['comboboxSelected'] = e.addClass('ex-select-visible');
				window['comboboxSelect'] = e.prev('select');
				window['comboboxSelectFake'] = e.next('.ex-select');
                window['comboboxSelectFakeTopScroll'] = window['comboboxSelectFake'].find('div.top');
                window['comboboxSelectFakeBottomScroll'] = window['comboboxSelectFake'].find('div.bottom');
                window['comboboxSelectFakeContentWrapper'] = window['comboboxSelectFake'].find('.height');
                window['comboboxSelectFakeContent'] = window['comboboxSelectFake'].find('.slide');
				window['comboboxSelectFake'].show().css({left:offs.left-13, top:offs.top-11});
				window['comboboxSelectClose'] = window['comboboxSelectFake'].next('.ex-select-close').show().css({left:offs.left+offs_width+8, 
				top:offs.top+(offs_height/2)-13});
				
		};
	}


	$.fn.combobox = function(params){
		var tpl = {
			selected : '<a href="#" class="switches a_select" onClick="$.combobox(this); return false;"><span><span>{value}</span></span></a>',
			select : '<div class="div_options ex-select"><div class="top"></div><div class="bg">{option}</div></div>',
			exit : '<div class="ex-select-close" onClick="$.combobox(this,\'Close\');"></div>',
			cache : {}
		};

		tpl = $.extend(tpl, params);

		this.each(function(){
			tpl.cache = {};
			var optgroup = $(this).children('optgroup');
			var option = $(this).find('option');
			var column = $(this).data('column');
			var title = $(this).data('title');
			var fxw_width = 100/(column || 3);
			var result = '';
			
			var fi = 0;
			
			if(optgroup.length === 0){

				option.each(function(i,v){
					//alert(typeof option.each);
					
					if(v.selected)
					{
						tpl.cache.selected = tpl.selected.replace(/{value}/,(title || v.text));
						tpl.cache.option += '<div class="fxw" style="width:'+fxw_width+'%"><a href="#" class="this-option this-selected" data-value="'+v.value+'" onClick="$.combobox(this,\'onChange\',\''+(title || v.text)+'\',\''+(tpl.result || '')+'\'); return false;">'+v.text+'</a></div>';
						result = v.text;
					} else {
						tpl.cache.option += '<div class="fxw" style="width:'+fxw_width+'%"><a href="#" class="this-option" data-value="'+v.value+'" onClick="$.combobox(this,\'onChange\',\''+(title || v.text)+'\',\''+(tpl.result || '')+'\'); return false;">'+v.text+'</a></div>';
					}
						fi++;
						if(fi == (column || 3)) {
							tpl.cache.option += '<div class="clear"></div>';
							fi = 0;
						}
						
					tpl.cache.select = tpl.select.replace(/{option}/,tpl.cache.option).replace(/undefined/,'');
					
				});
			} else {
				
				tpl.cache.option = '';
                                optgroup.each(function(j,val) {
                                    tpl.cache.option += '<p>' + $(this).attr('label') + '</p><div class="optgroup">';
                                    $(this).children('option').each(function(i,v) {
                                        if(v.selected)
					{
						tpl.cache.selected = tpl.selected.replace(/{value}/,v.text);
						tpl.cache.option += '<div class="fxw" style="width:'+fxw_width+'%"><a href="#" class="this-option this-selected" data-value="'+v.value+'" onClick="$.combobox(this,\'onChange\'); return false;">'+v.text+'</a></div>';
					} else {
						tpl.cache.option += '<div class="fxw" style="width:'+fxw_width+'%"><a href="#" class="this-option" data-value="'+v.value+'" onClick="$.combobox(this,\'onChange\'); return false;">'+v.text+'</a></div>';
					}
					
                                    });
                                    tpl.cache.option += '</div>';
                                })
                                tpl.cache.select = tpl.select.replace(/{option}/,tpl.cache.option).replace(/undefined/,'');
			}

			if(tpl.result)
			{
				$(this).next(tpl.result).text(result).end().hide().after(tpl.cache.selected+tpl.cache.select+tpl.exit);
			} else { 
				$(this).hide().after(tpl.cache.selected+tpl.cache.select+tpl.exit);
			}
		});
		return this;
	};
	
$.fn.autoClear = function(){
	this.each(function(){
		$(this).attr('defaultvalue',$(this).val()).focus(function(){
			if($(this).val() == $(this).attr('defaultvalue'))
				$(this).val('');
		}).blur(function(){
			if($(this).val() == '')
				$(this).val($(this).attr('defaultvalue'));
		});
	});
}

	
})(jQuery);
