Flash={
	show:function(){
		$('#flashMessage').fadeIn(400, function(){
			this.timer=setTimeout('Flash.hide()',5000);
		});
	},
	hide:function(){
	  $('#flashMessage').fadeOut(400, function(){
			clearTimeout(this.timer);
			return false;
	  });
	}
};


var eventflag = '';  //just controller var, no special meaning but could become handy
var cookie_options = { path: '/', expires: 30 };

var belement = null; //current element to show the loader

function loadMap(func) {
  if(func == undefined || func == null) func = initialize_map;
  google.load("maps", "2", {"callback" : func});
}

function formSubmit(frm, load_msg){
	var FORM = document.getElementById(frm);
	var params = {};
	var tINPUTS = $(FORM)
	.find("input[checked], input[type='text'], input[type='password'], input[type='submit'], option[selected], textarea")
	.filter(":enabled")
	.each(function(){
		params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
	});//each
	
	$(FORM).find("input[type='hidden']").each(function(){
		params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
	});//each

	t_MSG(load_msg);
	
	$.ajaxSetup({dataType: "json"});
	
	$.post(FORM.action, params, function(data){
		onFormSuccess(data);
	});
	
}

function t_MSG(msg){
	
}

document.cropfunction = function(){};


function isEmail(text){
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(text);
}


function bindDefaultPlugins(){
	$('a.tipsy').tipsy({gravity: 's'});
	$('a.tipsyn').tipsy({gravity: 'n'});
	if (window['AddBindings']){
		AddBindings();
	}
	
	setThumbIcons();
}



function setThumbIcons(){
	//this function sets the appropriate icons to the thumbnail
	$('li.thumb').each(function(){
		$(this).children('div.icons').remove();
		if($(this).hasClass('video') || $(this).hasClass('error') || $(this).hasClass('private') || $(this).hasClass('default')){
		
			
			$(this).append('<div class="icons"></div>');
			if($(this).hasClass('video')){
				$(this).children('div.icons').append('<div class="ivideo"></div>');
			}
			if($(this).hasClass('error')){
				$(this).children('div.icons').append('<div class="ierror"></div>');
			}
			if($(this).hasClass('default')){
				$(this).children('div.icons').append('<div class="idefault"></div>');
			}
			if($(this).hasClass('private')){
				$(this).children('div.icons').append('<div class="iprivate"></div>');
			}
		}
	});
}


function showLoader(element, lclass){
	if(lclass==undefined) lclass="loader";
	$(element).after('<span class="'+lclass+'" id="a_'+element.id+'_loader"></span>');
}

function hideLoader(element){
	if(element != null)
	$('#a_'+element.id+'_loader').remove();
}


function hideOptMenu(){
	$('ul.optmenu').attr('menu_id', null).hide();
	opt_menu_id = null;
	clearTimeout(opt_menu_to);
	opt_menu_to = null;
	$('a.optmenu').removeClass('visible');
}

function optMenuInit(){
	
	$(".mitem").hover(function(){
		//$(".mitem").not(this).find('a.optmenu').removeClass('visible');
		var tit = $(this).find('a.optmenu').addClass('visible');
	}, function(){
		var el = $(this).find('a.optmenu');
		var to = setTimeout(function(){
				if(!opt_menu_id){
					el.removeClass('visible');
				}
			}, 50);
	});
	
	
	$('ul.optmenu').remove().clone(true).appendTo("body");
	
	$('a.optmenu').live("mouseover", function(){
		var menu = $('#'+$(this).attr('rel'));
		var el = $(this);
		var offset = el.offset();
		var mid = el.attr('rev');
		menu.css({left:offset.left-15, top:offset.top+13}).attr("menu_id", mid).show();
	}).live("mouseout", function(e){
		if(!opt_menu_to){
			opt_menu_to = setTimeout(function(){if(!opt_menu_id){ hideOptMenu();}}, 300);
		}
	});
	
	$('ul.optmenu').bind("mouseenter", function(){
		 opt_menu_id = $(this).attr("menu_id");
		 clearTimeout(opt_menu_to);
		 opt_menu_to = null;
	}).bind("mouseleave", function(e){
		hideOptMenu();
	});
}

var opt_menu_id = null;
var opt_menu_to = null;


$(document).ready(function(){
	
	bindDefaultPlugins();

	$(document).click(function(){
		$('#languages_menu').fadeOut('fast');
	});
	
	$("#lang_more").mouseover(function(){
		$('#languages_menu').fadeIn('fast');
	});
	
	
	$('#news_list td.vnews p b').click(function(){
		//var next = $(this).next();
		$(this).hide().next().css('display', 'inline');
	});
	
	
	optMenuInit();
	
	$("a.ajax, a.ajax > *").live("click", function(){
		
		//if(this.tagName != 'A')
		
		if(this.tagName == 'A'){
			var element = this;
		}else{
			var element = $(this).parents('a:first')[0];
		}
		
		var bid = $(element).attr('id');
		var brel = $(element).attr('rel');
		var brev = $(element).attr('rev');
		
		//check if has a class showloader, if yes, do show it
		if($(element).hasClass('showloader')){
			$(element).after('<span class="loader" id="a_'+bid+'_loader"></span>');
			$('#a_'+bid+'_loader').fadeIn('fast');
		}
		
		
		
		var doajax = true;
		
		if(brev != undefined && brev != ''){
			if(window[brev]){
				doajax = window[brev](element);
			}
		}
		
		if(doajax){
			$.get(element.href, function(data){
				if(brel != undefined && brel != ''){
					
					if(window[brel]){
						window[brel](data, element);
						bindDefaultPlugins();
					}else{
						if($(element).hasClass('remove')){
							$('#'+brel).fadeOut('slow', function(){
								$(this).remove();
							});
						}else{
							$('#'+brel).html(data);
							bindDefaultPlugins();
						}
					}
				}else{
					if(data == "false"){
						if(bid){
							var onFalseFunc = bid+"_onFalse";
							if (window[onFalseFunc]){
								onFalseFunc(element);	
							}else{
								alert('error');
							}
						}else{						
							alert('error');
						}
					}else{
						if(bid){
							var onTrueFunc = bid+"_onTrue";
							if (window[onTrueFunc]){
								window[onTrueFunc](data, element);	
							}else{
								alert(data);
							}
						}else{						
							alert(data);
						}
					}
					
				}
				
				if($(element).hasClass('showloader')){
					$('#a_'+bid+'_loader').fadeOut('fast').remove();
				}
				
			});
		}
		
		return false;
	});
	
	
	$( "a.thickbox, area.thickbox, input.thickbox").live("click", function(){
		var t = this.title || this.name || null;
		var a = this.href || this.alt;
		var g = this.rel || false;
		tb_show(t,a,g);
		this.blur();
		return false;
	});
	
	
	$(".ajaxform").live("submit", function(){
		if(window[this.id+'_formoptions'] != undefined){
			mioptions = window[this.id+'_formoptions'];
		}else{
			alert('Missing form options!');
			return false;
		}
		
		$(this).ajaxSubmit(mioptions);
		return false;
	});
	
	$('input[type=submit]').live("click", function(){
		$(this).attr("disabled", "disabled").blur();
		$('input').removeClass('submitbtn');
		$(this).addClass('disabled').addClass('submitbtn');
		$(this).after('<span class="loader" id="frm_loader"></span>');
		window['eventflag'] = $(this).attr('id'); //set the eventflag var, what button has been clicked
		$('#frm_loader').fadeIn('fast');
		
	});
	
	
	$("#crop").live("click", function(){	document.cropfunction();});
	
	
	$('ul.admin_menu').mouseenter(function(){
		var o = $(this);
		var am = o.clone().appendTo('body');
		am.addClass('clonemenu');
		am.css({left:o.offset().left, top:o.offset().top, position:"absolute"});
		o.hide();
		
		
		$('ul.clonemenu a').unbind('click').click(function(){
			var li = $(this).parent();
			var index = $('ul.clonemenu ul.admin_submenu a').index(this);
			var links = o.find('ul.admin_submenu a');
			am.trigger('mouseleave');
			$(links[index]).trigger('click');
			return false;
		});
		
		am.mouseleave(function(){
			$('ul.admin_menu').show();
			$('ul.clonemenu').remove();
		});
	});
	
	
	
	
	
	$('ul.admin_submenu a:not(.thickbox)').click(function(){
		var el = $(this);
		var val = '';
		
		if(el.attr('rev') != ''){
			val = prompt(el.attr('rev'), el.attr('rel'));
			if(val == el.attr('rel') || val==null) return false;
		}
		
		if(el.hasClass('deleteitem') && el.attr('rel') != ''){
			if(!confirm(el.attr('rel'))) return false;
		}
		
		el.parent().parent().siblings('a.admin').addClass('processing').text('processing...');
		
		
		
		$.getJSON(this.href+val, function(data){
			if(data.error != undefined){
				alert(data.error);	
			}else{
				//console.debug(data);
				
				if(window[data.success]){
					window[data.success](data, el);
				}
				
			}
			
			el.parent().parent().siblings('a.admin').removeClass('processing').text('');
		});
		
		
		return false;
	});
	
	
	 
});





function d(string){
	top.console.debug(string);
}

function dir(string){
	top.console.dir(string);
}

function toSlug(s, num_chars) {
	// changes, e.g., "Petty theft" to "petty_theft"
	// remove all these words from the string before urlifying
	//token from the django js function URLify
	removelist = ["a"];
	r = new RegExp('\\b(' + removelist.join('|') + ')\\b', 'gi');
	s = s.replace(r, '');
	s = s.replace(/[^\w\s-]/g, '');  // remove unneeded chars
	s = s.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces
	s = s.replace(/\s+/g, '_');      // convert spaces to underscores
	s = s.toLowerCase();             // convert to lowercase
	return s.substring(0, num_chars);// trim to first num_chars chars
}


//Rating plugin, by Damir Secki
(function($) {
	
	$.fn.daxRating = function(rateUrl) {
		
		// Scope
		var elements = this;
		var rateUrl = '';
		var c = 0;
		
		return(
			elements.each(function() {				
				
				var el = $(this);

				var linksHtml = '';
				var cid = $(this).closest("li").attr('id');
				rateUrl = '/casinos/rate/'+cid+'/';
				//add 5 links
				for(var i=1; i<6; i++){
					linksHtml += '<a rel="r'+i+'"href="'+rateUrl+i+'"></a>';
				}
				$(this).html(linksHtml);
				
				el.children('a').hover(function(){
					el.addClass($(this).attr('rel'));
					//top.console.debug('up');
				},function(){
					el.removeClass($(this).attr('rel'));
					//top.console.debug('down');
				}).click(function(){
					el.removeClass($(this).attr('rel'));
					el.addClass('r0');
					el.html('<span class="loader"></span> saving data');
					
					/*
					var t = $(this).attr('title');
					var a = $(this).attr('href');
					var g = false;
					tb_show(t,a,g);*/
					
					
					$.getJSON($(this).attr('href'), function(data){
						if(data.error != undefined){
							alert(data.error);
							el.html('').removeClass('r0');
						}else{
							
							el.html('').attr('title', 'Total votes:&nbsp; <b>'+data.total_rates+'</b><br />Average vote:&nbsp; <b>'+data.full_rate+'</b>').removeClass('r0').addClass('r'+data.rate).tipsy({gravity:'s'});
						}
					});
					return false;
				});
			})
		);
	}
})(jQuery);


//DefaultValue plugin
(function($) {
	
	$.fn.defaultvalue = function() {
		
		// Scope
		var elements = this;
		var args = arguments;
		var c = 0;
		
		return(
			elements.each(function() {				
				
				// Default values within scope
				var el = $(this);
				var def = args[c++];

				el.val(def).focus(function() {
					if(el.val() == def) {
						el.val("");
						el.addClass('regular');
					}
					el.blur(function() {
						if(el.val() == "") {
							el.removeClass('regular');
							el.val(def);
						}
					});
				});
				
			})
		);
	}
})(jQuery);


/*
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * http://stilbuero.de/jquery/cookie/
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
*/

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};