//Overwrite default alert function
//window.alert = alert_mod;

$(window).load(function() {
	//$(document).pngFix();
	if(typeof scroller_file != 'undefined') $('body').append('<img src="http://static.pazintys.lt/img/pixel.gif" width="1" height="36">');
}); 

function slide_up(id) {
	if($('#'+id+' ul').css('display') != 'none') {
		$('#'+id+' ul').slideUp('100');
		$(document).unbind('click');
	}
}
function slide_down(id) {
	if($('#'+id+' ul').css('display') == 'none') {
		$('#'+id+' ul').slideDown('100');
		setTimeout(function(){
			$(document).bind('click',function(){
				slide_up(id)
			})
		},600);
	}
}

$(document).ready(function(){
    
	/*
    $('.hotline').innerfade({
        animationtype: 'slide',
        speed: 'slow',
        timeout: 10000,
        type: 'sequence',
        containerheight: '42px',
        pause : 1,
        nsfading : 1
    });
    */
    
	// find all the input elements with title attributes and make them with a hint
	$('input[title!=""]').hint('inactive');

	$('body').append('<div style="display:none" id="confirm_modal"></div><div style="display:none" id="dialog"></div>');

	init_confirmations();
    
	//Search options > advanced menu
	//$('#search_options').one('click',openMenu);
	//$('#search_options').click(openMenu);
	$('#search_options').bind("mouseenter",function(){
		$('.outerpair1').slideToggle(0).bind("mouseleave",function(){
			$(this).slideUp(0);
		});
	});
    
	//Search options popup menu actions
	$('.main_menu li a').click(function(){
		/* animated
         $('#search_option_block').animate({'background-color': 'yellow'}).load($(this).attr('rel'),'',function (){ $(this).animate({'background-color': 'none'}) });
         */
		$('#search_option_block').load($(this).attr('rel'));
		$('#search_option_text').text($(this).text().substring(2));
		closeMenu();
		return false;
	});

    

});

//Search options > advanced menu
function openMenu() {
	$('.outerpair1').slideToggle(0);
/*
    $('.outerpair1').show(0, function(){
        $(document).one('click',closeMenu);
        $(this).one('click', closeMenu);
    });
    */
}
function closeMenu() {
	$('.outerpair1').slideUp(0);
}
//End advanced menu

function init_confirmations() {
    
	var prefix = (arguments.length >= 1) ? arguments[0] : '';

	$(prefix + 'a.confirm, ' + prefix + ' a.confirm_tb, ' + prefix + ' a.confirm_js').click(function() {
        
		if($(this).attr('rel') == '') return false;
        
		var url = $(this).attr('href');

		var temp = $(this).attr('rel').split('|');
		var content = temp.length > 0 ? temp[0] : 'Confirm';
		var title = temp.length > 1 ? temp[1] : 'Warning';
		var ok_text = temp.length > 2 ? temp[2] : 'OK';
		var cancel_text = temp.length > 3 ? temp[3] : 'X';

		$('#confirm_modal').html(content);
		$('#confirm_modal').css({
			display:'block'
		});

		if($(this).hasClass('confirm_tb')) {
            
			eval("var buttons = { " +
				"'" + ok_text + "' : function() { $(this).dialog('destroy'); tb_show(null, url, null); }, " +
				"'" + cancel_text + "' : function() { $(this).dialog('destroy'); } " +
				"}"
				);
        
		} else if($(this).hasClass('confirm_js')) {
            
			eval("var buttons = { " +
				"'" + ok_text + "' : function() { $(this).dialog('destroy'); $.getScript(url); }, " +
				"'" + cancel_text + "' : function() { $(this).dialog('destroy'); } " +
				"}"
				);
        
		} else {
            
			eval("var buttons = { " +
				"'" + ok_text + "' : function() { $(this).dialog('destroy'); document.location.href=url; }, " +
				"'" + cancel_text + "' : function() { $(this).dialog('destroy'); } " +
				"}"
				);
		}
        
		$('#confirm_modal').dialog({
			modal: true,
			buttons: buttons,
			overlay: {
				opacity: 0.5,
				background: 'black'
			},
			title: title,
			width: '330px',
			height: '150px'
		});
        
		return false;
	});
}

function confirm_mod(content) {

	var title = (arguments.length >= 2) ? arguments[1] : 'Warning';
    
	var ok_func = (arguments.length >= 3) ? arguments[2] : null;
	var cancel_func = (arguments.length >= 4) ? arguments[3] : null;
    
	var ok_text = (arguments.length >= 5) ? arguments[4] : 'OK';
	var cancel_text = (arguments.length >= 6) ? arguments[5] : 'X';
    
	$('#confirm_modal').html(content);
	$('#confirm_modal').css({
		display:'block'
	});

	eval("var buttons = { " +
		"'" + ok_text + "' : function() { $(this).dialog('destroy'); "+(ok_func!=null?ok_func+'();':'')+"; return true;}, " +
		"'" + cancel_text + "' : function() { $(this).dialog('destroy'); "+(cancel_func!=null?cancel_func+'();':'')+"; return false;} " +
		"}"
		);
    
	$('#confirm_modal').dialog({
		modal: true,
		buttons: buttons,
		overlay: {
			opacity: 0.5,
			background: 'black'
		},
		title: title,
		width: '330px',
		height: '150px'
	});
}

function alert_mod(message) {
    
	var title = (arguments.length >= 2) ? arguments[1] : 'Warning';
	var button_text = (arguments.length >= 3) ? arguments[2] : 'OK';
    
	eval("var buttons = {'" + button_text + "' : function() { $(this).dialog('destroy'); }}");

	$('#dialog').html(message);
	$('#dialog').css({
		'display':'block'
	});
	$('#dialog').dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		width: '330px',
		height: '150px',
		resizable: false,
		draggable: false,
		buttons: buttons,
		overlay: {
			'opacity': 0.5,
			'background': 'black'
		},
		title: title
	});
	$('#dialog').dialog('open');
    
	return false;
}

//Togle block
function block_togle(what,who,togle){
	if (arguments.length == 2) {
		what.slideToggle('fast',
			function(){
				if ( $(this).is(':visible') )
					who.css('background-position','left top')
				else
					who.css('background-position','left bottom')
			});
	} else if (arguments.length >= 3) {
		what.slideDown('fast',
			function(){
				who.css('background-position','left top')
			});
	}
}

//Preload images
//example:  $.preloadImages("v3/profile/profile_2.png","v3/profile/video_2.png");
$.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		//alert('loading: '+arguments[i]);  //for debug
		jQuery("<img>").attr("src", arguments[i]);
	}
}

//Return param value - example: http://pazintys/main/profile/index.php#edit_login?what=bla
//This is for ajax history
//alert($.urlParam('what')); returns 'bla'
$.urlParam = function(name){
	var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
	return results[1] || 0;
}