Cufon.replace('h1, #navigation-main, #starter h3, #starter section.middle ul li a, #main h3, #partners li.label, #breadcrumbs li.label, footer h3, #schooling h4');
Cufon.replace('#slider div.buttons li');
Cufon.replace('.article h2, #newsletter h4, #article-header h2', { textShadow: '1px 1px rgba(51, 51, 51, 0.3)' });

$(function(){
	/*toggle upper footer*/
	$('footer span.tick').click(function(){
		$(this).next('div').slideToggle();
	});
	
	/*home page*/
	//news, multimedia
	$('#starter section.middle ul').tabs('#starter section.middle > div');
	//slider
	$('#slider div.article').scrollable({
		circular: true,
		mousewheel: true,
		vertical: true,
		next: '#slider a.arrow-down',
		prev: '#slider a.arrow-up',
		onBeforeSeek: function(event, i){
			var el = $('#t'+parseInt(i+1))
			var size = $('#slider div.buttons ul li').size();
			if(size == i) var el = $('#t1')
			el.parent('li').siblings().removeClass('current')
			el.parent('li').addClass('current');
			Cufon.refresh('#slider div.buttons li');
		}
	}).navigator({
		navi: '#slider div.buttons ul',
		naviItem: 'li',
		activeClass: 'current'
	}).autoscroll(5000);
	
	/*pagination*/
	$('#pager select').change(function(){
		window.open($(this).val(),'_self');
	});

	/*youtube.com gallery*/
    $('#yt-player').tubeplayer({
        width: $('#yt-initial').attr('data-width'),
        height: $('#yt-initial').attr('data-height'),
        initialVideo: $('#yt-initial').val()
    });
    $('#yt-gallery a').bind('click',function(e){
    	e.preventDefault();
        $(this).addClass('active').siblings('a').removeClass('active');
        $('#yt-player').tubeplayer('cue', $(this).attr('href').substr(31,$(this).attr('href').length));

	var text = $(this).parent("div.item").find("p.desc").text();
	$("#yt-description").text(text);
    });
    
	/*images gallery*/
	$('#gallery-box, div.scrollable-gallery').scrollable();

	/*fancybox*/
	$('a.fancybox').fancybox();
	$("div.output a:has(img):not(.fancybox)").click(function()
		{
		if($(this).attr('href'))
			{
			var arr = ['.jpg','jpeg','.gif','.png'];

			var ext = $(this).attr('href');
			ext = ext.substr(ext.length-4,4);

			if($.inArray(ext.toLowerCase(),arr) >=0 )
				{
				$.fancybox(
					{
					'href': $(this).attr('href'),
					'titleShow': false,
					'hideOnContentClick': true
					});

				return false;
				}
			else
				{
				return true;
				}
			}
		});

	/*input*/
	$('input.placeholder').focus(function(){
		if($(this).val() == $(this).attr('placeholder')){
			$(this).val('');
			$(this).removeClass('empty');
		}
	});
	$('input.placeholder').blur(function(){		
		if($(this).val() == ''){
			$(this).val($(this).attr('placeholder'));
			$(this).addClass('empty');
		}
	});

	/*submiting from*/
	$('a.[data-action="submit"]').click(function(e){
		e.preventDefault();
		$(this).parents('form').submit();
	});
	    
    /*ajax forms*/
	$('form.ajax').submit(function(e){
		e.preventDefault();
		$(this).find('.error:not(input, select, textarea)').hide();
		$(this).find('input.error, select.error, textarea.error').removeClass('error');
		var ajaxStatus = $(this).find('span.ajaxstatus');
		ajaxStatus.show();
		var form = $(this);
		var ajaxResponse = $(this).find('.ajaxresponse');
		ajaxResponse.removeClass('error').hide();
		ajaxResponse.find('p').empty();
		ajaxResponse.find('ul').empty();
		$.ajax({
			type: 'POST',  
			url: form.attr('action'),
			dataType: 'json',
			data: form.serialize(),
			success: function(data){
				if((data.saved != undefined  && !data.saved) || (data.sent != undefined  && !data.sent)){
					var exclude = ['sent', 'logged', 'index', 'edit', 'insertId'];
					$.each(data,function(index, value){
						if(form.find('*[name="'+index+'"]').size() > 0 && $.inArray(index, exclude) == -1){
							form.find('*[name="'+index+'"]').addClass('error')
							ajaxResponse.find('ul').append('<li>'+value+'</li>');
						}
					});
					ajaxResponse.addClass('error');
				}
				ajaxResponse.find('p').text(data.msg);
				ajaxResponse.show();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				ajaxResponse.find('p').addClass('error').text(textStatus);
				ajaxResponse.show();
			},
			complete: function(){
				//$.scrollTo("header",1000);
				ajaxStatus.hide();
			}
		});
	});
	//ocena artykułu
	$('#rate-article span.thumb').live('click',function(){
		$('#rate-article span.ajaxstatus').show();
		if(!$(this).hasClass('disabled')){
			$(this).siblings('.active').removeClass('active');
			$(this).addClass('active');
		}
		var url = $('#rate-article form').attr('action');
		var resource_type = $('#rate-article input[name="resource_type"]').val();
		var resource_id = $('#rate-article input[name="resource_id"]').val();
		if($(this).hasClass('thumb-up')){
			var rate = 1;
		}else{
			var rate = -1;
		}
		var dataString = 'resource_type='+resource_type+'&resource_id='+resource_id+'&rate='+rate;
		var ajaxResponse = $('div.rating .ajaxresponse');
		ajaxResponse.removeClass('error').hide();
		ajaxResponse.find('p').empty();
		ajaxResponse.find('ul').empty();
		$.ajax({
			type: 'POST',  
			url: url,
			dataType: 'json',
			data: dataString,
			success: function(data){
				if(data.saved){
					$('#rate-current').text(data.rate);
					$('#rate-article span.thumb').addClass('disabled');
				}else{
					ajaxResponse.addClass('error');
				}
				if(typeof data.logged != 'undefined') ajaxResponse.find('ul').append('<li>'+data.logged+'</li>');				
				ajaxResponse.find('p').text(data.msg);
				ajaxResponse.show();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				ajaxResponse.find('p').addClass('error').text(textStatus);
				ajaxResponse.show();
			},
			complete: function(){
				$('#rate-article span.ajaxstatus').hide();
			}
		});
	});

	/* szkolenia - szczegóły */
	$('ul.jtabs').tabs('div.jpanes > div',{current: 'active', tabs: 'li'});

	/* szkolenia - pokazywanie/ukrywanie części formularza o danych firmy */
	$('#schooling #schooling-company').click(function()
		{
		$('#schooling #schooling-company-data').slideToggle();
		});

	/* szkolenia - accordion na grupy szkoleń */
	$('#schooling div.jaccordion').tabs('#schooling div.jaccordion div.pane', {tabs: 'div.groups', effect: 'slide'});

	/* szkolenia - odczyt terminów */
	$('#schooling input[name="schooling_id"]').change(function()
		{
		$('#schooling div.times p.info').hide();
		$('#schooling div.times-content').hide();
		$('#schooling div.times span.ajaxstatus-times').show();

		$.ajax({
			type: 'GET',
			url: $(this).attr('rel'),
			dataType: 'html',
			success: function(data)
				{
				$('#schooling div.times-content').html(data).show();
				},
			error: function(XMLHttpRequest, textStatus, errorThrown)
				{
				},
			complete: function()
				{
				$('#schooling div.times span.ajaxstatus-times').hide();
				}
			});
		});

	/* Google Maps */
	var lat = $('#localization-lat').val();
	var lng = $('#localization-lng').val();
	if($('#interactiveMap').size() > 0){
		new google.maps.Map(document.getElementById('interactiveMap'), {
	        center: new google.maps.LatLng(lat, lng),
	        zoom: 13,
	        mapTypeId: google.maps.MapTypeId.ROADMAP
	    });
	}
	if($('#gmaps').size() > 0){
		var mapResourceUrl = $('#mapResourceUrl').val();
		var mapDiv = document.getElementById('gmaps');
	    var map = new google.maps.Map(mapDiv, {
	        center: new google.maps.LatLng(lat, lng),
	        zoom: 13,
	        mapTypeId: google.maps.MapTypeId.ROADMAP
	    });
	    var bounds = new google.maps.LatLngBounds();
	    
	    $.ajax({
			type: "GET",
			url: mapResourceUrl,
			dataType: "xml",
			success: function(xml) {			
				$(xml).find('objects marker').each(function(index, value){
					var title = $(this).find('title').text();
					var description = $(this).find('description').text();
					var lat = $(this).find('latLng').attr('lat');
					var lng = $(this).find('latLng').attr('lng');
					var icon = $(this).find('icon').text();
					
					var marker = new google.maps.Marker({
						position: new google.maps.LatLng(lat, lng),
						title: title,
						map: map,
						icon: icon
					});
					var infowindow = new google.maps.InfoWindow({
						content: "<table class='infowindow'>" +
		                 "<tr><td><h4>"+title+"</h4></td></tr>" +
		                 "<tr><td>"+description+"</td></tr>" +
		                 "</table>"	
					});
					
					google.maps.event.addListener(marker, 'click', infoCallback(infowindow, marker, map));
					
					bounds.extend(new google.maps.LatLng(lat, lng));
				});
				
				$(xml).find('objects polyline').each(function(){
					var name = $(this).find('title').text();
					var description = $(this).find('description').text();
					
					var polylineOptions = {
				        strokeColor : $(this).find('strokeColor').text(),
				        strokeOpacity : 1,
				        strokeWeight : $(this).find('strokeWeight').text()
				    };
				    
				    var polyline = new google.maps.Polyline(polylineOptions);
				    
				    polyline.setMap(map);
					
					$(this).find('latLng').each(function(){
						polyline.getPath().push(new google.maps.LatLng($(this).attr('lat'), $(this).attr('lng')));
						bounds.extend(new google.maps.LatLng($(this).attr('lat'), $(this).attr('lng')));
					})
					
					var infowindow = new google.maps.InfoWindow({
						content: "<table class='infowindow'>" +
		                 "<tr><td><h4>"+name+"</h4></td></tr>" +
		                 "<tr><td>"+description+"</td></tr>" +
		                 "</table>"	
					});
					
					google.maps.event.addListener(polyline, 'click', function(event) {
						infowindow.position = event.latLng;
						infowindow.open(map);
					});
				});
				
				$(xml).find('objects polygon').each(function(){
					var name = $(this).find('title').text();
					var description = $(this).find('description').text();
				    
					var polygonOptions = {
				        strokeColor : $(this).find('strokeColor').text(),
				        strokeOpacity : 1,
				        strokeWeight : $(this).find('strokeWeight').text(),
				        fillColor: $(this).find('fillColor').text()
				    };
				    var polygon = new google.maps.Polygon(polygonOptions);
				    
				    polygon.setMap(map);
					
					$(this).find('latLng').each(function(){
						polygon.getPath().push(new google.maps.LatLng($(this).attr('lat'), $(this).attr('lng')));
						bounds.extend(new google.maps.LatLng($(this).attr('lat'), $(this).attr('lng')));
					})					
					
					var infowindow = new google.maps.InfoWindow({
						content: "<table class='infowindow'>" +
		                 "<tr><td><h4>"+name+"</h4></td></tr>" +
		                 "<tr><td>"+description+"</td></tr>" +
		                 "</table>"	
					});
					
					google.maps.event.addListener(polygon, 'click', function(event) {
						infowindow.position = event.latLng;
						infowindow.open(map);
					});       
				});
				
				if(!bounds.isEmpty()){
					map.fitBounds(bounds);
					if (map.getZoom() > 15) {
				    	map.setZoom(15);
				  	}
			  	}
			}
		});
	}

	/* tooltip */
	$(".jtooltip").tooltip({
		offset: [-3, 0]
	});

	/* partnerzy - scroller */
	$('#partners div.scrolling').jStockTicker({interval:20});
});

//otwieranie chmurki(infowindow) przy markerze
function infoCallback(infowindow, object, map) {
	return function() {
		infowindow.open(map, object);
	};
}
