$(document).ready(function(){

	DD_belatedPNG.fix('.widget-weather-page img');
	
	$('a.widget-button').each(widgetButton);
	$('input.js-style').each(widgetInputStyle);
	$('button.widget-input-button').each(widgetFormButton);
	$('.widget-input-button').each(widgetButton);
	$('.widget-gallery-preview').each(addTitle);
	
	$('#weather-tabs').tabs();
	
	$('#jcarousel-gallery').jcarousel({
		scroll: 1,
		wrap: 'circular',
		auto: 0,
		visible: 1,
		buttonPrevHTML : '<span class="icon previous">Poprzednia</span>',
		buttonNextHTML: '<span class="icon next">Nastêpna</span>'
	});
	
	$('#jcarousel-large-gallery, #jcarousel-video').jcarousel({
		scroll: 1,
		auto: 0,
		visible: 4,
        offset:0,
        vertical:false,
		buttonPrevHTML : '<span class="icon previous">Poprzednia</span>',
		buttonNextHTML: '<span class="icon next">Nastêpna</span>'
	});
	
	$('#jcarousel-video a').click(openMediumVideo);
	$('#jcarousel-video a:first').trigger('click');
	
});

function widgetButton() {
	$(this).addClass('left-bg');
	$(this).append('<span class="right-bg">&nbsp;</span>');
}

function widgetFormButton() {
	var self = $(this);
	var text = self.text() || self.val();
		
	self = $('<a>').insertAfter(this).addClass(this.className).attr('id', this.id).css({'cursor' : 'pointer'}).text(text);
	$(this).remove();
	self.click(function() {
		self.parents('form').submit();
	});
}

function widgetInputStyle() {
	var self = $(this);
	self.wrap('<div class="input-left-bg">').addClass(this.className).attr('id', this.id);
	self.after('<div class="input-right-bg">');

}

function addTitle() {
	var image = $(this).children('img');
	var title = image.attr('title');
	var width = image.width();
	$(this).append( $('<span>').addClass('preview-title').css('width', width).html('<em>' + title + '</em>') );
}

function openMediumVideo() {
	var url = $(this).attr('href');
	var title = $(this).find('img').attr('alt');
	$('#jcarousel-video a.preview').remove();
	$('#jcarousel-video').prepend('<a class="preview" href=""><img class="border" src="' + url + '" alt="' + title + '" /></a>');
	
	return false;
}
