function showMessage(msg){
	alert(msg);
}

function askQuestion(msg){
	return window.confirm(msg);
}

function isJSON(str){
	return str.substring(0,2)=='({' && str.substring(str.length-2,str.length)=='})';
}

function redirect(url){
	location.href = url;
}

(function($){
	
	$.fn.muffledInput = function(){
		return this.each(function(){
			var $this = $(this), value = $this.val();
			
			$this.focus(function(){
				$this.val('');
				$this.removeClass('muffled-input');
			});
			
			$this.blur(function(){
				if ($this.val()=='') $this.val(value);
				if (value==$this.val()) $this.addClass('muffled-input');
			});
			
		});
	}
	
	$.fn.expandUL = function(){
		return this.each(function(){
			var $ul = $(this);
			
			$ul.find('a[href$="#"]').unbind('click').click(function(){
				var $child = $(this).parents('li:first').find('> ul');
				if ($child.is(':visible')) $child.hide(); else $child.show();
				return false; 
			});
		});
	}
	
	$.fn.showLoading = function(){
		return this.each(function(){
			var $this = $(this), $box = $this.find('div.loading-box');
			if ($box.size()){
				$box.show();
			} else {
				$this.prepend('<div class="loading-box"><div class="mask"></div><div class="body"><div class="box"></div></div></div>');
				$box = $this.find('div.loading-box');
				$box.show();
			}
			var $l = $box.find('div.box');
			var t = ($this.innerHeight()-$l.innerHeight())/2+'px', l = ($this.innerWidth()-$l.innerWidth())/2+'px';
			$l.css({'left':l,'top':t});
		});
	}
	
	$.fn.hideLoading = function(){		
		return this.each(function(){
			$(this).find('div.loading-box').hide();
		});
	}
	
	$.fn.zebra = function(){
		return this.each(function(){
			var $this = $(this);
			var ch = '*';
			switch (this.tagName){
				case 'UL':
				case 'OL':
					ch = '> li';
				break;
				case 'TABLE':
					ch = '> tbody > tr';
				break;
				case 'THEAD':
				case 'TBODY':
				case 'TFOOT':
					ch = '> tr';
				break;
			}
			$this.find(ch).removeClass('even odd');
			$this.find(ch+':even').addClass('even');
			$this.find(ch+':odd').addClass('odd');
		});
	}

	$.fn.sortPosition = function(url,fn){
		return this.each(function(){
			$(this).sortable({'stop':function(){
				var arr = [], $this = $(this);
				$this.find('> li').each(function(){
					arr[arr.length] = $(this).attr('value');
				});
				$this.showLoading();
				$.post(url,{'ajax':1,'pos':arr},function(data){
					$this.hideLoading();
					if (fn && typeof fn=='function'){
						fn.call($this.get(0),data);
					}
				});
			}});
		});
	}
	
	var initVideoPlayerID = 0;
	
	$.fn.initVideo = function(){
		return this.each(function(){
			var $this = $(this);
			initVideoPlayerID++;
			var flashvars =  {
				file	: $this.attr('href'),
				image	: $this.find('> img').attr('src')
			};
			var params = {
				allowfullscreen		: 'true',
				allowscriptaccess	: 'always',
				bgcolor				: '#ffffff'
			};
			var attributes = {
				id		: 'player_'+initVideoPlayerID,
				name	: 'player_'+initVideoPlayerID
			};
			$this.html('<div id="div_player_'+initVideoPlayerID+'"></div>').show().css('display','block');
			swfobject.embedSWF('/swf/player.swf', 'div_player_'+initVideoPlayerID, '500', '400', '9.0.124', 
				false, flashvars, params, attributes);
			
			$this.attr('href','#').unbind('click').click(function(){
				return false;
			})
		});
	}
	
	$.fn.popupVideo = function(){
		return this.each(function(){
			var $this = $(this);
			
			$this.unbind('click').click(function(){
				return false;
			});
		});
	}
	
	$.fn.downloadDialog = function(){
		return this.each(function(){
			var $this = $(this);
			
			function _closeDownload(){
				$('#downloader').hide();
			}
			
			function _moveDownload(){
				var $dlg = $('#downloader'), $body = $dlg.find('> .body');
				$body.css({
					'top'	: (getWindowScroll('top')+40)+'px'
				});
			}
			
			function _showDownload(){
				var $dlg = $('#downloader'), $mask = $dlg.find('> .mask');
				$dlg.show();
				$dlg.height(getWindowSize('height')+'px');
				
				$mask.click(function(){
					_closeDownload();
				});
				
				$dlg.find('a.close').unbind('click').click(function(){
					_closeDownload();
					return false;
				});
				
				$(window).resize(function(){
					_moveDownload();
				}).scroll(function(){
					_moveDownload();
				});
				
				_moveDownload();
			}
			
			function _drawDownload(){
				var $dlg = $('#downloader');
				if (!$dlg.size()){
					$('body').prepend('<div id="downloader"><div class="mask"></div><div class="body"><div class="a"><a href="#" class="close"></a></div><div class="scroll"></div></div></div>');
					$dlg = $('#downloader');
				}
				return $dlg;
			}
			
			$this.unbind('click').click(function(){
				var $dlg = _drawDownload(), $body = $dlg.find('> .body > .scroll');
				_showDownload();
				if ($body.html()=='') $body.load($this.attr('href'),{'ajax':1});
				return false;
			});
		});
	}
	
	$.fn.questionMarks = function(){
		return this.each(function(){
			var $p = $(this).parent(), $i = $p.find('> img.js-question-mark');
			
			$p.css('position','relative');
			$(this).hover(function(){
				$p.find('> div.js-question-answer').css({
					'left':$i.position().left+$i.width()+'px','top':'-12px'}).show();;
			},function(){
				$p.find('> div.js-question-answer').hide();;
			});
		});
	}
	
	$(document).ready(function(){
		$('.js-muffled-input').muffledInput();
		$('.js-ul-expander').expandUL();
		$('.zebra').zebra();
		$('.js-question-mark, .js-question-answer').questionMarks();
		$('a.gallery').lightBox({
			imageLoading:			'/img/jquery-lightbox/ico-loading.gif',
			imageBtnPrev:			'/img/jquery-lightbox/btn-prev.gif',
			imageBtnNext:			'/img/jquery-lightbox/btn-next.gif',
			imageBtnClose:			'/img/jquery-lightbox/btn-close.gif',
			imageBlank:				'/img/jquery-lightbox/blank.gif'
		});
		
		$('a.player').initVideo();
		
		//$('a.popup-video').popupVideo();
		
		$('a.download-dialog').downloadDialog();
		
		$('.js-creeping').creeping({'delay':100, 'height':'20px'});
	});
	
})(jQuery)