function isDefined(variable)
{
    return (typeof(window[variable]) == "undefined") ? false : true;
}

function ifNotDefined(variable, value)
{
	return (isDefined(variable)) ? window[variable] : value;
}

function switchPhoto(uri)
{
    document.getElementById('galPic').src = uri + '/large.jpg';
}

function login ()
{
    $('#login').toggleClass('login');
}

function hide(id)
{
    $('#'+id).toggle();
}

function hide_profile(id)
{
    $('#'+id).toggle();
    
    if($('#'+id).css('display') == 'block')
    {
        $('#detailsh').html(i18n[0]);
    }
    else
    {
        $('#detailsh').html(i18n[1]);   
    }
}

function changemonth(month,id)
{
    var currentTime = new Date();
    var Year = currentTime.getFullYear();
    var Day  = currentTime.getDate()-1;
    var offset = 0;
    
    if(Year % 4 == 0)
        if(Year % 100 != 0)
            offset = 1;
        else if(Year % 400 == 0)
            offset = 1;

    var Months = [31,28+offset,31,30,31,30,31,31,30,31,30,31];
    
    document.getElementById(id).options.length = 0;
    for(var i = 0;i<Months[month-1];i++)
    {
    
        document.getElementById(id).options[i] = new Option(i+1,i+1);
    }

    document.getElementById(id).selectedIndex = Day <= i ? Day : 0;
}

function changecountry(country, id, selected, parentElem)
{          
    $.post(
        http_location+'/index/loadcities', 
        {countryId: country},
        function(data)
        {
            $('#'+id)[0].options.length = 0;
            $('#'+id)[0].options[0] = new Option('', '');
            for(var i = 0;i < data.length; i++)
            {
                $('#'+id)[0].options[i+1] = new Option(data[i][1], data[i][0]);
            }
            if(selected)
            {
                $('#'+id).val(selected);
            }            
        },
        'json'
    );
    if(parentElem)
    {
        $('#'+parentElem).val(country);
    }
}

function changeroutes(obj)
{
    $('a[type=routes_selector]').each(function()
    {
        $(this).removeClass('sel');
    });
    $('ol[class=list]').html('');    
    $(obj).addClass('sel');
    $('#loader').show();

    $.post(
        http_location+'/index/loadroutes', 
        {type: $(obj).attr('val')},
        function(data)
        {
            $('#loader').hide();        
            $('ol[class=list]').html(data);
            $.post(
                http_location+'/index/loadroutescount', 
                {type: $(obj).attr('val')},
                function(data)
                {
                    $('#routes_counter').html(data);
                }
            );             
        }
    );    
}

function prepareAdType()
{
	var val = $('#direction').val();
	var period = $('#periodical').val();
    if(val == 1 || period == 2)
    {
        $('#departure_home').hide();
    }
    else
    {
        $('#departure_home').show();
    }
    if(period == 2)
    {
        $('p.periodicity').hide();
        $('*[attr=show]').hide();
    }
    
        $('#departure_to').hide();
        $('#departure_home').hide();    
    $('#direction').change(function()
    {
        var value = $(this).val();
		var period = $('#periodical').val();
		if(period == 2)
			return;
        if(value == 1)
        {
            $('#departure_home').hide();
        }
        else
        {
            $('#departure_home').show();
        }
    }
    )
    $('#courier').click(function(){
        if ($('#courier').is(':checked'))
        {
            $('#package').show();
            $('#package2').show();
        }
        else
        {
            $('#package').hide();
            $('#package2').hide();
        }
    }
    );
    /*
	$('#map_route, #free_route').click(function(){
		if ($('#map_route').is(':checked'))
		{
			$('dl.dl1').hide();
		}
		else
		{
			$('dl.dl1').show();
		}
	});
    */
}

function preparePeriodicity()
{
	container = $('p.periodicity');
	container.hide();
	handle = $('<a></a>');
	handle.append('<img src="' + config.mediaLocation + '/img/ico_calendar.png" />');
	handle.click(function(){
		container.toggle();
        $('#periodical').val(container.css('display') == 'none' ? 0 : 1);
	});
	container.after(handle);
    
    $('#periodical').change(function(){
        
        $('#departure_to').hide();
        $('#departure_home').hide();    
            
        var show = $('*[attr=show]');
            
		if($(this).val() == 1)
        {
			container.show();
            show.show();
            $('#departure_to').show();
            if($('#direction').val() == 0)
                $('#departure_home').show();
            else
                $('#departure_home').hide();            
        }
        else if($(this).val() == 0)
        {
            container.hide();
            show.show();
            $('#departure_to').show();
            
            if($('#direction').val() == 0)
                $('#departure_home').show();
            else
                $('#departure_home').hide();
        }   
        else if($(this).val() == 2)
        {
            container.hide();
            show.hide();
            $('#departure_to').hide();            
            $('#departure_home').hide();
        }
    });    
}

function registration()
{   
    var pass = $('#userspassword_tmp').val();

    $('#userspassword_1').val(pass);
    $('#userspassword_2').val(pass);
    
    $('input[name=UsersType]').click(function()
    {
        changeUserType($(this).val());
    }
    );   
}
function changeUserType(value)
{
    if(value == 'passenger')
    {
        $('#field_userscar').hide();
        $('#field_userslicense').hide();
        $('#field_userscarseats').hide();
        $('label[for=userscar]').parent().hide();
        $('label[for=userslicense]').parent().hide();
        $('label[for=userscarseats]').parent().hide();
        
        $('label[for=userslicensetypes]').parent().hide();        
        $('label[for=userslicensetypes]').parent().next().hide();                
    }
    else
    {
        $('#field_userscar').show();
        $('#field_userslicense').show();          
        $('#field_userscarseats').show();
        $('label[for=userscar]').parent().show();
        $('label[for=userslicense]').parent().show();
        $('label[for=userscarseats]').parent().show();        
        
        $('label[for=userslicensetypes]').parent().show();        
        $('label[for=userslicensetypes]').parent().next().show();        
    }
}

function prepareHeadingToggle(idSet)
{
	for (var i = 0; i < idSet.length; i++)
	{
		var id = idSet[i];
		$('#' + id + '_heading').click(function(){
			$(this).toggleClass('open');
			$(this).toggleClass('closed');
			$('#' + id + '_box').toggle();
		});
	}
}

function GoToMapFromRegister()
{
    //alert(1);
    $('form[id=form]').attr('action', http_location + '/map/user');
    $('form[id=form]').submit();
}

$(function() {    


    $("#message2").dialog({
        autoOpen: false,
        bgiframe: true,
        resizable: false,
        height:60,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        }
    });

    $("#dialog2").dialog({
        autoOpen: false,
        bgiframe: true,
        resizable: false,
        height:60,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        }
    });
    $("#dialog3").dialog({
        autoOpen: false,
        bgiframe: true,
        resizable: false,
        height:60,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        }
    });
});

