$(document).ready(function() 
{
	$("#headerSearchQuery").click(function() {
		$("#headerSearchQuery").val('');
	});

	$("#headerSearchQuery").blur(function() {
		if ( $(this).val() == '' )
			$(this).val(SEARCH_INPUT_VALUE);
	});
	
	$("#headerSearchSend").click(function() {
		if ( $("#headerSearchQuery").val().length > 2 && $("#headerSearchQuery").val() != SEARCH_INPUT_VALUE ) {
			window.location = base_url + 'wyszukiwarka/' + $("#headerSearchQuery").val();
			return false;
		}
		
		return false;
	});
		
	$("#add_photo").click(function() {
		$("#upload_pics").show();
		$("#gallery_row").hide();
	});
	
	$("#s_msg_close").click(function() {
		$("#s_message").hide('slow');
	});
	
	$("#newsletter-choice-branch").click(function() {
		var email = $.trim($("#email-index-newsletter").val());
		
		if ( email.length >= 3 )
			window.location = base_url + 'biuletyn-zapisz-sie/' + email;
		else
			$("#newsletter-index-status").html('<img src="' + base_url + 'res/_img/site/cross.png" style="vertical-align: middle" alt="ERROR" /> Wpisz adres e-mail.');
	});
});

function showIndexNewsletter()
{
	var email = $.trim($("#email-index-newsletter").val());
	
	if ( email.length >= 3 )
	{
		$("#newsletter-choice-branch").show();
	}
}

function showIndexIcon(id, option)
{
	if ( option == 'onmouseout' )
		$("#ii_" + id).attr('src', base_url + 'res/_img/index/50/' + id + '.png');
	else if ( option = 'onmouseover' )
		$("#ii_" + id).attr('src', base_url + 'res/_img/index/70/' + id + '.png');
}

function loadOptionDescription(optionID)
{
	var optionsRows = new Array;
	
	optionsRows[0] = 'free';
	optionsRows[1] = 'premium';
	optionsRows[2] = 'premiumplus';
	optionsRows[3] = 'gold';
	
	for ( i = 0; i < optionsRows.length; i++ ) {
		if ( optionID != optionsRows[i] ) {
			$("#description_"+ optionsRows[i]).hide();
			$("#sub_"+ optionsRows[i]).hide();
		}
	}
	
	switch ( optionID )
	{
		case 'free':
			$("#num_steps").text('3');
		break;
		
		case 'premium':
		case 'premiumplus':
		case 'gold':
			$("#num_steps").text('4');
	}
	
	$("#description_" + optionID).show('slow');
	$("#sub_"+ optionID).show('slow');
}

function checkUserField(name, value)
{
	if ( value != '' )
	{
		$.ajax({
			url:	base_url + 'ajax/user/check',
			type:	'POST',
			data:	'field=' + name + '&value=' + value,
			
			success:	function(request) {
				if ( request == 'OK' ) {
					$("#input_" + name).removeClass('ajax-input-error').addClass('ajax-input-good');
				}
				else {
					$("#input_" + name).removeClass('ajax-input-good').addClass('ajax-input-error');
				}
				
				$('#request_data').html('');
			},
			beforeSend: function() {
				$('#request_data').html('<img src="' + base_url + 'res/_img/site/loader.gif" alt="loading" />');
			}
		});
	}
}

function showCategoryResult(cid)
{
	if ( cid != 0 )
	{
		$.ajax({
			url:	base_url + 'ajax/ask/main',
			type:	'POST',
			data:	'cid=' + cid,
			
			success:	function(request) 
			{
				if ( request == 'EMPTY_ROW' ) {
					$('#request_data').attr('style', 'color: #ff0000').html('Brak dostawców dla tej branży. Wiadomość nie zostanie wysłana.');
				}
				else {
					$('#request_data').attr('style', '').html(request);
				}
			},
			beforeSend: function() {
				$('#request_data').html('<img src="' + base_url + 'res/_img/site/loader.gif" alt="loading" />');
			}
		});
	}
}

function rateImage(rate_id, cid, bg, module, option, row_id)
{
	if ( option == 'onmouseover' )
	{
		$.each([1, 2, 3, 4, 5], function(index, value) {
			$('#r_' + value +'_' + cid + '_' + module).attr('src', base_url + 'res/_img/site/star_white.png');
		});
	}

	$.each([1, 2, 3, 4, 5], function(index, value) {
		if ( rate_id >= value ) {
			$('#r_' + value +'_' + cid + '_' + module).attr('src', base_url + 'res/_img/site/star_gold.png');
		}
	});
	
	if ( option == 'onmouseout' )
	{
		var currentRange = $("#" + module +"_" + row_id).attr('title');
			outputRange  = currentRange.split(':');
			
		if ( outputRange[0] != '0' && outputRange[1] != '0' )
		{
			var result = Math.round( (outputRange[0] / outputRange[1]));
			
			$.each([1, 2, 3, 4, 5], function(index, value) {
				$('#r_' + value +'_' + cid + '_' + module).attr('src', base_url + 'res/_img/site/star_white.png');
			});
			
			$.each([1, 2, 3, 4, 5], function(index, value) {
				if ( result >= value ) {
					$('#r_' + value +'_' + cid + '_' + module).attr('src', base_url + 'res/_img/site/star_gold.png');
				}
			});			
		}
		else
		{
			$.each([1, 2, 3, 4, 5], function(index, value) {
				$('#r_' + value +'_' + cid + '_' + module).attr('src', base_url + 'res/_img/site/star_white.png');
			});	
		}
	}
}

function rateCompany(cid, range, module)
{
	$.ajax({
		url:	base_url + 'ajax/rating/vote',
		type:	'POST',
		data:	'cid=' + cid + '&range=' + range +'&module=' + module,
		
		success: function(msg) 
		{
			if ( msg == 'ERROR' )
				$("#vote_msg").html('');
			else if ( msg == 'GLOS_ODDANY')
				$("#vote_msg").attr('style', 'color: #ff0000').html('Głosowałeś(łaś) już.');
			else if ( msg == 'OK' )
			{
				$("#vote_msg").attr('style', 'color: #ff0000').html('<img src="' + base_url + 'res/_img/site/accept.png" alt="OK" />');
				
				$.each([1, 2, 3, 4, 5], function(index, value) {
					$('#ra_' + value +'_' + cid + '_' + module).attr('onclick', '').attr('onmouseover', '').attr('onmouseout', '');
				});
				
				rateImage(range, cid, 'gold', module);
			}
		},
		beforeSend: function() {
			$("#vote_msg").html('<img src="' + base_url + 'res/_img/site/loader.gif" alt="loading" />');
		}
	});
}

function contactShowSubject(c_id)
{
	if ( c_id == 5 )
		$("#contact_subject").show();
	else
		$("#contact_subject").hide();
}

DD_roundies.addRule('.dodaj-ogloszenie-index-button', '5px', true);
DD_roundies.addRule('#newsletter-choice-branch', '5px', true);
DD_roundies.addRule('.brb_info', '5px', true);
DD_roundies.addRule('.mn_content', '5px', true);
//DD_roundies.addRule('.branch-list-home-index', '0px 4px 4px 4px', true);
//DD_roundies.addRule('.search-index-show', '4px 4px 0px 0px', true);
DD_roundies.addRule('.header_login', '4px 4px 0px 0px', true);
DD_roundies.addRule('.error_box', '5px', true);
DD_roundies.addRule('#s_message', '5px', true);
DD_roundies.addRule('.right-newsletter', '5px', true);
DD_roundies.addRule('#sort_bg', '5px', true);
DD_roundies.addRule('.sort_inside', '5px', true);
DD_roundies.addRule('#branch-index-list-inside', '5px', true); // index
DD_roundies.addRule('.branch-list', '5px', true); // katalog firm
DD_roundies.addRule('.moreDbLink', '5px', true); // dashboard more link
DD_roundies.addRule('.option_module', '5px', true); // katalog firm wizytowka
DD_roundies.addRule('.static_main_view', '5px 5px 0px 0px');
