// Search fields in the header and footer
// Should submit on click on form and should not on click in textfield
function stopIt(e) {
	try {
		window.event.cancelBubble = true;
	}
	catch (err) {
		e.stopPropagation();
	}
}



// Dropdown menu
dropdownMenu = function() {
		if (document.all&&document.getElementById) {
				var navRoot = document.getElementById("navRoot");
				for (i=0; i<navRoot.childNodes.length; i++) {
						var node = navRoot.childNodes[i];
						if (node.nodeName=="LI") {
								node.onmouseover=function() {
										this.className+=" over";
										
										//console.log(this.className);
								
								}
								node.onmouseout=function() {
										this.className=this.className.replace("over", "");
										
										//console.log(this.className);
								}
						}
				}
		}
}

// @element String - Pass identifier of a search box that you wish to toggle
// @handle String - Pass identifier of a handle that will show search box on click
function toggleSearch(element, handle) {
	var el = $(element);
	var hndl = $(handle);
	el.hide(); // Initially the seach box should be hidden
	hndl.click(function(){ el.toggle(); return false; });
}


function tabs() {
	var tabs = ".tabs";
	var tabsContent = ".tabs-content";
	
	$("#content "+tabsContent).not("#tab1").hide();
	$(tabs+" a[href=#tab1]").addClass("current");
	
	$(tabs+" a").click(function(){
		// Hide other tabs
		$("#content "+tabsContent).hide();
		// Show requested
		var target = $(this).attr("href").substr(1);
		$("#content #"+target+tabsContent).show();
		//Reset active tab handler
		$(tabs+" a").removeClass("current");
		//Set new active tab handler
		$(this).addClass("current");
		
		return false;
	});
}
	
	
	
// Projects slider
function projectSlider() {
	if (!$('#project .browser')) { return; }
	
	//$('#project .browser li:gt(0)').hide();
	
	var a = '';
	var ind = 0;
	var caption = $('#project .caption');
	var cur = $('#project .browser .viewport'); // Besides it's also the current link
	var curImg = cur.find('img'); // Current image
	var frames = $('#project .slides-nav li');
	var curInd = frames.filter(':first'); // Current slider indicator
	curInd.addClass('first');
	frames.filter(':last').addClass('last');
	
	// Helper function
	_showNewFrame = function() {
		a = $(frames[ind]).find('a'); // Get current link
		cur.attr('href', a.attr('href')); // Set new link address
		curImg.attr('src', a.text()); // Set new image source
		caption.html(a.attr('title')); // Set the new caption
		curImg.load(function() { curImg.fadeIn(300); }); // Show it
	}
	
	$('#project a.next').click(function(e){
		if (ind < (frames.length-1)) {
			curImg.fadeOut(300); // Hide current image
			setTimeout(function() { // Wait until the image is hidden
				ind++;
				_showNewFrame();
				
				// This part is for the additional frame navigation
				curInd.removeClass('active'); 
				curInd = curInd.next()
				curInd.addClass('active');
			}, 300);
		}
		
		return false;
	});
	
	$('#project a.prev').click(function(e){
		if (ind > 0) {
			curImg.fadeOut(300);
			setTimeout(function() {
				ind--;
				_showNewFrame();
				
				// This part is for the additional frame navigation
				curInd.removeClass('active');
				curInd = curInd.prev()
				curInd.addClass('active');
			}, 300);
		}
			
		return false;
	});
	
	frames.find('a').click(function(e){
		var a = $(this);

		// Activate curent slide indicator
		curInd.removeClass('active'); // Deactivate old indicator
		curInd = a.parent(); // Get the new indicator
		curInd.addClass('active'); // Activate new indicator
		
		ind = frames.index(curInd); // Get the new frame index
		
		// Show slide of given index
		curImg.fadeOut(300);
		setTimeout(function() {
			_showNewFrame();
		}, 300);
		
		return false;
	});
	
	/*$('#project a.next').click(function(e){
		if (!cur.hasClass('last')) {
			cur.fadeOut(300); // Hide current image
			setTimeout(function() { // Wait until the image is hidden
				cur = cur.next(); // Get the next item
				cur.fadeIn(300); // Show it
				
				// This part is for the additional frame navigation
				curInd.removeClass('active'); 
				curInd = curInd.next()
				curInd.addClass('active');
			}, 300);
		}
		
		return false;
	});*/
	
	/*$('#project a.prev').click(function(e){
		if (!cur.hasClass('first')) {
			cur.fadeOut(300);
			setTimeout(function() {
				cur = cur.prev();
				cur.fadeIn(300);
				curInd.removeClass('active');
				curInd = curInd.prev()
				curInd.addClass('active');
			}, 300);
		}
			
		return false;
	});*/
}


// first prominent line on top of the home page
function catchlines() {
	var cur = $('#catchline ul li:first');
	$('#catchline ul li:last').addClass('last');
	
	$('#catchline ul li:gt(0)').hide();
	$('#catchline .refresh').click(function(e){
		cur.fadeOut(250);
		setTimeout(function() {
			if (cur.hasClass('last')) {
				cur = $('#catchline ul li:first');
			} else {
				cur = cur.next();
			}
			cur.fadeIn(250);
		}, 250);
		
		return false;
	});
}


function jqm_prepare() {
	if ($.jqm) {
		// Setting up global parameters
		$.jqm.params.modal = false; // Display all dialogs as modal
		
		// Initializing modal wondows
		$('.jqm-dialogs').jqm({trigger:'false', toTop:'ture'});
		
		$('#content .jqm-triggers').click(function() {
				var dialog = $(this).attr('href').substr(1); // Find out jqm-dialog id
				$('#'+dialog).jqmShow();
				return false;
		});
	}
}


jQuery(function($){
		dropdownMenu();
		tabs();
		projectSlider();
		toggleSearch("#nav form.search", "#nav .search-toggle");
		toggleSearch("#nav form.search", ".search-toggle-2"); // link on the 404 page
		catchlines();
		jqm_prepare();
		
		/*// Navigation drop shadow text
		$('#nav a, #subnav a, #head .phone').each(function(){$(this).textDropShadow();});
		
		// Zebra table
		$('table.data tr:even').addClass('even');*/
});
