// MAESTRO UI JAVASCRIPT 1.2 for jQuery
// Vladimir Strackovski, 10/30/2011
//--------------------------------------

$(document).ready(function(){
	
	showtime();		

	$('body').append('<div id="pageFade"></div>');				
	setTimeout(function(){
		$('#pageFade').animate({opacity: 0}), 250;
		setTimeout(function() {
			$('#pageFade').remove();		
		}, 250);
	}, 250);			
	
	// Main navigation hover effect
	if($("#topnav li"))
	{
		var topNav = $("#topnav li");		
		$(topNav).prepend("<span></span>"); //Throws an empty span tag right before the a tag	
		$(topNav).each(function() { //For each list item...
			var linkText = $(this).find("a").html(); //Find the text inside of the <a> tag
			$(this).find("span").show().html(linkText); //Add the text in the <span> tag
			$(this).find("a").css('color', '#d4f400');			
		}); 
	
		$(topNav).hover(function() {	//On hover...
			$(this).find("span").stop().animate({
				marginTop: "-50" //Find the <span> tag and move it up 40 pixels
			}, 250);
		} , function() { //On hover out...
			$(this).find("span").stop().animate({
				marginTop: "0"  //Move the <span> back to its original state (0px)
			}, 250);
		});	
		
		$(topNav).click(function() {	
			$(this).find("a").addClass('selected');
		});			
	}	
	
	function showtime(){						
		// DELETE ???
		if($('.section-entry')){
			$(function() {
				$( ".section-entry" ).accordion({
					collapsible: true
				});
			});	
		}		
		
		jQuery.fn.exists = function(){return jQuery(this).length>0;}
				
		// TIMETABLE GENERATOR
		$('#timetable').click(function(){
			if($('.tt_city').exists()){
				$('.tt_city').remove();	
				$(this).css('background', 'url(../css/images/right_arrow.png) no-repeat 15px center');															
				$('#timetable').css('color', '#999');					
			}
			else{
				var cities=new Array();
				cities[0]="Ljubljana";      
				cities[1]="Maribor";
				cities[2]="Kranj";				
				cities[3]="Zagorje";								
				cities[4]="Koper";															
				
				for (var i = 0; i < cities.length; i++) {
					$('<li class="tt_city">' + cities[i] + '</li>').insertAfter('#timetable');								
				}			
				$('#timetable').css('color', '#7aaf36');																																			
				$('#pricelist').css('color', '#999');									
				$(this).css('background', 'url(../css/images/down_arrow-gre.png) no-repeat 15px center');																			
				$('#pricelist').css('background', 'url(../css/images/right_arrow.png) no-repeat 15px center');																											
			}			
			if($('.pl_city').exists()){
				$('.pl_city').remove();	
			}
				
			$('.tt_city').click(function(){			
				if($(this).hasClass('selected-subitem')){
					// do nothing
				}
				else
				{
					$('#section-menu ul .selected-subitem').removeClass('selected-subitem');				
					$(this).addClass('selected-subitem');	
					$('#ttable').animate({opacity: 0}, 300);							
					$('.entry-content').animate({opacity: 0}, 400);		
					$('<span class="loading-c"><p>Nalagam / Loading...</p><img src="../css/images/loadingAnimation.gif" alt="Nalagam..." /></span>').insertAfter('.main-content-panel aside');														
					$('.loading-c').animate({opacity: 1}, 150);																	
					var selectedCity = $(this).text();						
					setTimeout(function() {
						var url = window.location.pathname;	
						if(url.indexOf("/en/") != -1){
							$.get('../data/timetable/en_' + selectedCity + '.html', function(data){
									$('.entry-content').html(data);																
							})
							.success(function() { 
								$('.main-content-panel .loading-c').animate({opacity: 0}, 300);						
								setTimeout(function(){						
									$('.entry-content').animate({opacity: 1}, 300);											
									$('#ttable').animate({opacity: 1}, 300);										
								}, 300);
							})
							.error(function() {  
							
							})								
						}
						else{													
							$.get('../data/timetable/' + selectedCity + '.html', function(data){
									$('.entry-content').html(data);																
							})
							.success(function() { 
								$('.main-content-panel .loading-c').animate({opacity: 0}, 300);						
								setTimeout(function(){						
									$('.entry-content').animate({opacity: 1}, 300);											
									$('#ttable').animate({opacity: 1}, 300);										
								}, 300);
							})
							.error(function() {  
							
							})	
						}
					}, 400);
				}
			});					
		});		
		
		// PRICELIST GENERATOR
		$('#pricelist').click(function(){
			if($('.pl_city').exists()){
				$('.pl_city').remove();	
				$(this).css('background', 'url(../css/images/right_arrow.png) no-repeat 15px center');															
				$('#pricelist').css('color', '#999');					
			}
			else{
				var cities=new Array();
				cities[0]="Ljubljana";      
				cities[1]="Maribor";
				cities[2]="Kranj";				
				cities[3]="Zagorje";								
				cities[4]="Koper";															
				
				for (var i = 0; i < cities.length; i++) {
					$('<li class="pl_city">' + cities[i] + '</li>').insertAfter('#pricelist');								
				}
			
				$('#pricelist').css('color', '#7aaf36');																																			
				$('#timetable').css('color', '#999');									
				$(this).css('background', 'url(../css/images/down_arrow-gre.png) no-repeat 15px center');																			
				$('#timetable').css('background', 'url(../css/images/right_arrow.png) no-repeat 15px center');																											
			}			
			if($('.tt_city').exists()){
				$('.tt_city').remove();	
			}
				
			$('.pl_city').click(function(){			
				if($(this).hasClass('selected')){
					// ...
				}
				else
				{	var defaultContent = $('.entry-content').html();
					$('#section-menu ul .selected-subitem').removeClass('selected-subitem');				
					$(this).addClass('selected-subitem');							
					$('#ttable').animate({opacity: 0}, 300);							
					$('.entry-content').animate({opacity: 0}, 400);	
					$('<span class="loading-c"><p>Nalagam / Loading...</p><img src="../css/images/loadingAnimation.gif" alt="Nalagam..." /></span>').insertAfter('.main-content-panel aside');																															
					$('.loading-c').animate({opacity: 1}, 150);						
					var selectedCity = $(this).text();						
					setTimeout(function() {			
						var url = window.location.pathname;	
						if(url.indexOf("/en/") != -1){																					
							$.get('../data/pricelist/en_' + selectedCity + '.html', function(data){
								$('.entry-content').html(data);																
							})
							.success(function() { 
								$('.main-content-panel .loading-c').animate({opacity: 0}, 300);		
								$('.main-content-panel .loading-c').remove().delay(300);											
								setTimeout(function(){
									$('.entry-content').animate({opacity: 1}, 300);																		
									$('#ttable').animate({opacity: 1}, 300);										
								}, 300);
							})
							.error(function() {  
								$('.main-content-panel .loading-c p').html('<span style="color: red; font-weight: bold;">Ups, prišlo je do napake!</span><p>Zahtevane vsebine ni mogoče najti, vendar bomo zadevo v kratkem uredili. Hvala za razumevanje!</p>');											
								$('.main-content-panel .loading-c').animate({opacity: 1}, 300);																																								
								setTimeout(function(){
									$('.main-content-panel .loading-c').animate({opacity: 0}, 300);								
								}, 2000);				
								setTimeout(function(){							
									$('.entry-content').html('<img src="../css/images/no-content-bg-01.png" alt="Ni vsebine..." />');
									$('.entry-content').animate({opacity: 1}, 300);																		
									$('.main-content-panel .loading-c').remove();
									$('#section-menu ul .selected-subitem').removeClass('selected-subitem')																
								}, 2000);
						})	
						}
						else{
							$.get('../data/pricelist/' + selectedCity + '.html', function(data){
								$('.entry-content').html(data);																
							})
							.success(function() { 
								$('.main-content-panel .loading-c').animate({opacity: 0}, 300);		
								$('.main-content-panel .loading-c').remove().delay(300);											
								setTimeout(function(){
									$('.entry-content').animate({opacity: 1}, 300);																		
									$('#ttable').animate({opacity: 1}, 300);										
								}, 300);
							})
							.error(function() {  
								$('.main-content-panel .loading-c p').html('<span style="color: red; font-weight: bold;">Ups, prišlo je do napake!</span><p>Zahtevane vsebine ni mogoče najti, vendar bomo zadevo v kratkem uredili. Hvala za razumevanje!</p>');											
								$('.main-content-panel .loading-c').animate({opacity: 1}, 300);																																								
								setTimeout(function(){
									$('.main-content-panel .loading-c').animate({opacity: 0}, 300);								
								}, 2000);				
								setTimeout(function(){							
									$('.entry-content').html('<img src="../css/images/no-content-bg-01.png" alt="Ni vsebine..." />');
									$('.entry-content').animate({opacity: 1}, 300);																		
									$('.main-content-panel .loading-c').remove();
									$('#section-menu ul .selected-subitem').removeClass('selected-subitem')																
								}, 2000);
							})							
						}
					}, 400);							
				}
			});					
		});			

		$('#article-menu ul li').click(function(){
			var linkTarget = $(this).text().toLowerCase();																												
			if($(this).hasClass('selected')){
				// do nothing	
			}
			else{
				$('#article-menu ul .selected').removeClass('selected');
				$(this).addClass('selected');

				$('.entry-content').animate({opacity: 0}, 300);					
				$('<span class="loading-c"><p>Nalagam / Loading...</p><img src="../css/images/loadingAnimation.gif" alt="Nalagam..." /></span>').insertAfter('.main-content-panel aside');														
				$('.loading-c').animate({opacity: 1}, 150);									
				setTimeout(function() {
					$('.entry-content').empty();
					$.get('../data/about/' + $.trim(linkTarget) + '.html', function(data){
						$('.entry-content').html(data);
					})
					.success(function() {
						$('.main-content-panel .loading-c').animate({opacity: 0}, 300);						
						setTimeout(function(){
							$('.main-content-panel .loading-c').remove();
							$('.entry-content').animate({opacity: 1}, 300);
							showtime();							
						}, 300);
					})
					.error(function(){
						showtime();														
					})						
				}, 300);
				
			}
		});
		
//		if($('#article-menu ul')){
//			$('#article-menu ul li').click(function(){
//				var target = $(this).text();
//				if($(this).hasClass('selected')){	
//					// do nothing
//				}
//				else
//				{
//					$('#article-menu ul .selected').removeClass('selected');
//					$(this).addClass('selected');
//					$('.entry-content').animate({opacity: 0}, 300);	
//					setTimeout(function() {
//						$('.entry-content').empty();		
//						$.get('../data/about/' + target.toLowerCase() + '.php', function(data) {	
//						$('.entry-content').html(data);		
//						})
//						.success(function() { 																																		
//							$('.entry-content').animate({opacity: 1}, 300);																																								
//							showtime();							
//						})
//						.error(function() { showtime(); })
//					}, 300);					
//				}
//			});
//		}
		
		// Media widget controls
		if($('#entry-media-carousel')){			
			$('.entry-media-list a').mouseenter(function(){		
				$(this).attr('href', 'javascript:void(0);');					
				if($(this).find('img').attr('class') == 'video'){
					$(this).find('em').css('background', '#fff url(../css/images/video-icon.png) no-repeat center left');
				}			
			});																					
								
			$('#entry-media-carousel li').click(function(){
				if($(this).find('img').attr('title') && $(this).find('img').attr('class') == 'video'){
					embedLink = $(this).find('img').attr('title') + '?rel=0';
					$('.entry-media-content img').hide();			
					$('.entry-media-content iframe').remove();												
					$('.entry-media-content').animate({height:"310px"}, 200);				
					$('.entry-media-content').append('<iframe width="549" height="279" src="" frameBorder="0" scrolling="no" allowfullscreen></iframe>');
					$('.entry-media-content iframe').attr('src', embedLink);				
					$('.entry-media-content iframe').show();
				}
				else{
					imageLink =  $(this).find('img').attr('src');
					imageLink = imageLink.substring(imageLink.lastIndexOf('/')+1);
					$('.entry-media-content img').animate({opacity: 0}, 200);	
					setTimeout(function(){																						
					$('.entry-media-content iframe').attr('src', '');								
					$('.entry-media-content iframe').remove();						
					$('.entry-media-content').animate({height:"290px"}, 200);								
					$('.entry-media-content img').attr('src', '../images/content/' + imageLink);								
					$('.entry-media-content img').show();								
					}, 200);
					setTimeout(function(){
						$('.entry-media-content img').animate({opacity: 1}, 200);		
					}, 400);
				}
			});
		}
		
		// Featured/Ad carousel
		if($('#featured_carousel')){
			function mycarousel_initCallback(carousel)
			{
				carousel.buttonNext.bind('click', function() {
					carousel.startAuto(0);
				});
				carousel.buttonPrev.bind('click', function() {
					carousel.startAuto(0);			
				});
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, function() {
					carousel.startAuto();
				});
			};
		
			jQuery('#featured_carousel').jcarousel({
				scroll: 1,
				auto: 10,
				wrap: 'circular',
				initCallback: mycarousel_initCallback
			});
		}
		
		if($('#gallery-carousel')){
			function mycarousel_initCallback(carousel)
			{
				carousel.buttonNext.bind('click', function() {
					carousel.startAuto(0);
				});
				carousel.buttonPrev.bind('click', function() {
					carousel.startAuto(0);			
				});
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, function() {
					carousel.startAuto();
				});
			};
		
			jQuery('#gallery-carousel').jcarousel({
				scroll: 1,
				auto: 0,
				wrap: 'circular',
				initCallback: mycarousel_initCallback	
			});
		}		
		
		
		// Media entry carousel
		if($('#entry-media-carousel')){
			function mycarousel_initCallback(carousel)
			{
				carousel.buttonNext.bind('click', function() {
					carousel.startAuto(0);				
				});		
				carousel.buttonPrev.bind('click', function() {
					carousel.startAuto(0);			
				});
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, function() {
					carousel.startAuto();
				});
			};
				
			jQuery('#entry-media-carousel').jcarousel({
				scroll: 2,
				auto: 0,
				wrap: 'circular',
				initCallback: mycarousel_initCallback,
				itemFallbackDimension: 300		
			});
		}	
	
		// Applications form validator
		$("#inputForm").validate({
		 rules: {
			birthmonth: {
				required: true									
			},
			birthyear: {
				required: true									
			},
			birthday: {
				required: true									
			},
			stopnjaZnanja: {
				required: true									
			},
			krajTecaja: {
				required: true									
			},
			vrstaPlesa: {
				required: true									
			},
			starostnaSkupina: {
				required: true									
			}																	
			},
		 messages: {
		   fname: "Prosim vnesi ime.",
		   city: "Prosim vnesi kraj.", 
		   email: {
		   required: "Prosim vnesi e-poštni naslov.",
		   email: "Vnešen e-poštni naslov ni veljaven."
	   
		   },
		   lname: "Prosim vnesi priimek.",
		   address: "Prosim vnesi naslov.",
		   mobile: "Prosim vnesi kontaktno številko.",
		   birthday: "Prosim izberi dan rojstva.",
		   birthmonth: "Prosim izberi mesec rojstva.",
		   birthyear: "Prosim izberi leto rojstva.",
		   stopnjaZnanja: "Stopnjo izberi vsaj okvirno.",
		   krajTecaja: "Prosim izberi kraj tečaja.",
		   vrstaPlesa: "Izberi ples, ki te zanima.",
		   starostnaSkupina: "Izberi starostno skupino."		   			   	   		   
		 },
		errorPlacement: function(error, element){
			if(element.attr("name") == "fname"){
				error.appendTo($('#fname_error'));
				$('#fname_error').css('opacity', 0);													
				$('#fname_error').css('display', 'block');				
				$('#fname_error').animate({opacity: 1}, 400);				
			}
			
			if(element.attr("name") == "mobile"){
				error.appendTo($('#mobile_error'));
				$('#mobile_error').css('opacity', 0);
				$('#mobile_error').css('display', 'block');				
				$('#mobile_error').animate({opacity: 1}, 400);					
			}		
			
			if(element.attr("name") == "address"){
				error.appendTo($('#address_error'));
				$('#address_error').css('opacity', 0);
				$('#address_error').css('display', 'block');	
				$('#address_error').animate({opacity: 1}, 400);						
			}		
			
			
			if(element.attr("name") == "lname"){
				error.appendTo($('#lname_error'));
				$('#lname_error').css('opacity', 0);
				$('#lname_error').css('display', 'block');		
				$('#lname_error').animate({opacity: 1}, 400);						
			}		
			
			if(element.attr("name") == "email"){
				error.appendTo($('#email_error'));
				$('#email_error').css('opacity', 0);
				$('#email_error').css('display', 'block');	
				$('#email_error').animate({opacity: 1}, 400);							
			}	
			
			if(element.attr("name") == "city"){
				error.appendTo($('#city_error'));
				$('#city_error').css('opacity', 0);
				$('#city_error').css('display', 'block');					
				$('#city_error').animate({opacity: 1}, 400);					
			}			
			if(element.attr("name") == "birthmonth" || element.attr("name") == "birthyear" || element.attr("name") == "birthday"){
				error.appendTo($('#birthdate_error'));
				$('#birthdate_error').css('opacity', 0);
				$('#birthdate_error').css('display', 'block');			
				$('#birthdate_error').animate({opacity: 1}, 400);					
			}
			
			if(element.attr("name") == "address"){
				error.appendTo($('#address_error'));
				$('#address_error').css('opacity', 0);
				$('#address_error').css('display', 'block');				
				$('#address_error').animate({opacity: 1}, 400);				
			}				
			
			if(element.attr("name") == "mobile"){
				error.appendTo($('#mobile_error'));
				$('#mobile_error').css('opacity', 0);
				$('#mobile_error').css('display', 'block');					
				$('#mobile_error').animate({opacity: 1}, 400);					
			}
			if(element.attr("name") == "starostnaSkupina"){
				error.appendTo($('#starostnaSkupina_error'));
				$('#starostnaSkupina_error').css('opacity', 0);
				$('#starostnaSkupina_error').css('display', 'block');				
				$('#starostnaSkupina_error').animate({opacity: 1}, 400);					
			}	
			if(element.attr("name") == "vrstaPlesa"){
				error.appendTo($('#vrstaPlesa_error'));
				$('#vrstaPlesa_error').css('opacity', 0);
				$('#vrstaPlesa_error').css('display', 'block');	
				$('#vrstaPlesa_error').animate({opacity: 1}, 400);	
			}	
			if(element.attr("name") == "krajTecaja"){
				error.appendTo($('#krajTecaja_error'));
				$('#krajTecaja_error').css('opacity', 0);
				$('#krajTecaja_error').css('display', 'block');					
				$('#krajTecaja_error').animate({opacity: 1}, 400);					
			}											
			if(element.attr("name") == "stopnjaZnanja"){
				error.appendTo($('#stopnjaZnanja_error'));
				$('#stopnjaZnanja_error').css('opacity', 0);
				$('#stopnjaZnanja_error').css('display', 'block');	
				$('#stopnjaZnanja_error').animate({opacity: 1}, 400);						
			}					
		}
		});
	}
});
