/***************************************************************************************
*
*		NAVIGATION
*
***************************************************************************************/

function CheckRegex(str, typ) {
	if (typ == 'godzina')    var regex = /^[0-9]{1,2}[/:]{1}[0-9]{1,2}$/;
	else if (typ == 'data')  var regex = /^[0-9]{4}(-[0-9]{2}){2}$/;
	else if (typ == 'email') var regex = /^[a-zA-Z0-9.\-_]+@[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,4}$/;
	else return false;
	if (regex.test(str) == true) return true;
	else return false;
}

		$(document).ready(function(){
			$("#nav ul").css({display: "none"}); // Opera Fix
			$("#nav li").hover(function(){
				$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(200);
			},function(){
				$(this).find('ul:first').css({visibility: "hidden"});
			});
		});
		
/***************************************************************************************
*
*		ACCORDION PANELS
*
***************************************************************************************/

		$(document).ready(function(){
			$(".accordion").accordion ({
				header: "h4"
			});
		});

/***************************************************************************************
*
*		TOGGLE PANELS
*
***************************************************************************************/
		
		$(document).ready(function(){
			$(".toggle div").hide(); // hide div on default
			$(".toggle h4").click(function(){ // set the trigger
				$(this).toggleClass("active").next().slideToggle(300); // add class active and toggle speed
				return false;
			});
		});
		
/***************************************************************************************
*
*		TABS PANELS
*
***************************************************************************************/

		$(document).ready(function() {
		
			//When page loads...
			$(".tab-content").hide(); //Hide all content
			$("ul.tabs li:first").addClass("active").show(); //Activate first tab
			$(".tab-content:first").show(); //Show first tab content
			
			$("ul.tabs li:first-child").addClass("first-child");
			$("ul.tabs li:last-child").addClass("last-child");
			
			//On Click Event
			$("ul.tabs li").click(function() {
		
				$("ul.tabs li").removeClass("active"); //Remove any "active" class
				$(this).addClass("active"); //Add "active" class to selected tab
				$(".tab-content").hide(); //Hide all tab content
				
				var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
				$(activeTab).fadeIn(); //Fade in the active ID content
				return false;
			});
		
		});	

/***************************************************************************************
*
*		ADD ODD AND EVEN CLASS TO THE TABLE TR
*
***************************************************************************************/
		
		$(document).ready(function() {
			
			$("table tr:odd").addClass("odd");
			$("table tr:even").addClass("even");
		
		});	
		
/***************************************************************************************
*
*		ADDING CLASSES TO LI'S
*
***************************************************************************************/
		
		$(document).ready(function() {
			
			$("ul li:first-child").addClass("first-child");
			$("ul li:last-child").addClass("last-child");
		
		});	

/***************************************************************************************
*
*		FANCYCAPTION PLUGIN FOR OVERLAYS
*
***************************************************************************************/
function FancyInit()
{
	$(".fancycaption").fancyCaption();
}
		
/***************************************************************************************
*
*		FANCYBOX GALLERY PLUGIN
*
***************************************************************************************/
function LightBoxInit()
{
	$("a.fancybox-photo").fancybox({
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.7
	});
}
		$(document).ready(function(){
			LightBoxInit();
		});

/***************************************************************************************
*
*		FLICKR FEED
*
***************************************************************************************/
/*		
		$(document).ready(function(){
			$('#flickr-feed ul').jflickrfeed({
				feedapi: 'photoset.gne',
				limit: 8,
				qstrings: {
					nsid: '10729228@N07',
					set: '72157624798241210'
				},
				itemTemplate:
				'<li>' +
					'<a class="flickr-feed-photos" rel="group2" href="{{image}}" title="{{title}}">' +
						'<img src="{{image_s}}" alt="{{title}}" width="38" height="38" />' +
					'</a>' +
				'</li>'
			}, function(data) {
				$("a.flickr-feed-photos").fancybox({
					'overlayColor'		: '#000',
					'overlayOpacity'	: 0.7,
					'cyclic'            : true
				});
			});
		});
*/		
/***************************************************************************************
*
*		TWITTER FEED
*
***************************************************************************************/

		$(document).ready(function(){
			$('#tweets').tweetable({
				username: 'mthemes', 
				time: true, 
				limit: 1, 
				replies: true, 
				position: 'append'
			});
		});
		
/***************************************************************************************
*
*		TOTOP PLUGIN
*
***************************************************************************************/

		$(document).ready(function() {
			
			$().UItoTop({
				scrollSpeed: 500,
				easingType: 'easeOutQuart' 
	 		});
			
		});
		
/***************************************************************************************
*
*		TESTIMONIAL PLUGIN
*
***************************************************************************************/
		$(document).ready(function() {
		
			$('.testimonial').quovolver();
		
		});

/***************************************************************************************
*
*		NEWSLETTER INPUT
*
***************************************************************************************/
		
		$(document).ready(function() {  
			$('.newsletter input[type="text"], .sidebar-search input[type="text"]').addClass("normalfield");  
			$('.newsletter input[type="text"], .sidebar-search input[type="text"]').focus(function() {  
				$(this).removeClass("normalfield").addClass("focusfield");  
				if (this.value == this.defaultValue){  
					this.value = '';  
				}  
				if(this.value != this.defaultValue){  
					this.select();  
				}  
			});  
			$('.newsletter input[type="text"], .sidebar-search input[type="text"]').blur(function() {  
				$(this).removeClass("focusfield").addClass("normalfield");  
				if ($.trim(this.value == '')){  
					this.value = (this.defaultValue ? this.defaultValue : '');  
				}  
			});  
		});

/***************************************************************************************
*
*		PORTFOLIO SORTING
*
***************************************************************************************/

		$(document).ready(function(){
			
			//$('A').removeAttr('rel');
			$('.fancybox-photo').attr('rel', 'wszystko');
			//$(document).ready(function(){
				FancyInit();
			//});
			
			$(".filter li").removeClass("first-child");
			$(".filter li").removeClass("last-child");	

			// Clone portfolio items to get a second collection for Quicksand plugin
			var $portfolioClone = $(".portfolio-filterable").clone();
			
			// Attempt to call Quicksand on every click event handler
			$(".filter a").click(function(e){
				
				$(".filter li").removeClass("current");	
				
				// Get the class attribute value of the clicked link
				var $filterClass = $(this).parent().attr("class");

				if ( $filterClass == "all" ) {
					var $filteredPortfolio = $portfolioClone.find("li");
				} else {
					var $filteredPortfolio = $portfolioClone.find("li[data-type~=" + $filterClass + "]");
				}
				
				// Call quicksand
				$(".portfolio-filterable").quicksand( $filteredPortfolio, { 
					duration: 800, 
					easing: 'easeInOutQuad'
				}, function(){
					//$(document).ready(function(){
						FancyInit();
						LightBoxInit();
					//});
				});

				$(this).parent().addClass("current");

				// Prevent the browser jump to the link anchor
				e.preventDefault();
			});
			
			$('.FormularzKontaktowy').click(function(){
				var imi = $('INPUT[name="name"]').val();
				var ema = $('INPUT[name="email"]').val();
				var tem = $('INPUT[name="phone"]').val();
				var tre = $('TEXTAREA[name="comments"]').val();
				if ((imi.length > 0) && (ema.length > 0) && (tre.length > 0)) {
					if (CheckRegex(ema, 'email')) {
						$.ajax({
							type: "POST",
							url : "ajax/sendMail.ajax.php",
							data: "imi=" + imi + "&ema=" + ema + "&tem=" + tem + "&tre=" + tre,
							success: function(odp) {
								if (odp == 'ok') {
									$('INPUT[name="name"]').val('');
									$('INPUT[name="email"]').val('');
									$('INPUT[name="phone"]').val('');
									$('TEXTAREA[name="comments"]').val('');
									alert('Wiadomość zostałą wysłana.');
								}
								else alert('BŁĄD PODCZAS WYSYŁANIA!\n' + odp);
							},
							error  : function() {
								alert('Wystąpił błąd podczas komunikacji!');
							}
						});
					} else alert('Błędny format adresu e-mail!')
				} else alert('Nie wszystkie pola zostały wypełnione!');
			});
	
		});
