// Define variables
var remake = 0;
var remake_info = 0;
var black_something = 0;
var to_top_btn = 0;

// Ready
jQuery().ready(function() {
    $("a[rel^='prettyPhoto']").prettyPhoto();
    
    // Change height
	/*
	if ($('#sidebar').length && $('#content').length)
	{
		if ($('#sidebar').height() > $('#content').height())
		{
			$('#content').css('height', $('#sidebar').height() + 'px' );
		}
	}
	*/
     
    $('#home-info form fieldset input').datepicker({ firstDay: 1, minDate: 0, dateFormat: "yy-mm-dd", showOn: "button", buttonImage: _root+"web/images/live/btn-date.png" });
    
    // Headers
    if ($('#from_there .jcarousel-control').length)
    {
        $('#from_there').jcarousel({
            auto: 5,
            scroll: 1,
            wrap: 'both',
            animation: 'slow',
            initCallback: mycarousel_initCallback,
            buttonNextHTML: null,
            buttonPrevHTML: null,
            itemVisibleInCallback: {
                onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation
            }
        });
    }
    
    // Get Weather information
    get_weather();
    
    // Placeholder
    $('.placeholder').each(function(){
        var title = $(this).attr('title');
        var content = $(this).val();
        if (content == '')
        {
            $(this).val(title);
        }
    }).bind('focus', function() {
        var content = $(this).val();
        var title = $(this).attr('title');
        if (content == title)
        {
            $(this).val('');
        }
    }).bind('blur', function() {
        var content = $(this).val();
        var title = $(this).attr('title');
        if (content == '')
        {
            $(this).val(title);
        }
    });
    
    $('.article .stm').hover(function() {
        $(this).animate({opacity: '0.8'}, 200);
    },
    function() {
        $(this).animate({opacity: '1'}, 100);
    });
});

// Banners
function mycarousel_initCallback(carousel) {
    jQuery('#from_there .jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
};
function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
    $('#from_there .jcarousel-control a').removeClass('active');

    var active = idx-1;
    $('#from_there .jcarousel-control a:eq('+active+')').addClass('active');
    //console.log('Item #' + idx + ' is now visible');
};

function get_weather() {
    var weather = $('#header .weather');
    
    // Weather codes http://portal.nap.bg/

    $.YQL = function(query, callback) {
        var encodedQuery = encodeURIComponent(query.toLowerCase()),
            url = 'http://query.yahooapis.com/v1/public/yql?q='+ encodedQuery + '&format=json&callback=?';
        $.getJSON(url, callback);
    };
    
    $.YQL("select * from rss where url='http://weather.yahooapis.com/forecastrss?p=BUXX0004&u=c'",function(data){
        var w = data.query.results.item;
        var condition = w.condition.text;
        var temp = w.condition.temp;
        var encodedclass = condition.replace(/\s+/g, '-').toLowerCase();
        
        $('span em', weather).html(temp);
        $('div', weather).addClass(encodedclass);
        weather.animate({opacity: 1});
   });
}
