// 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();
	}
}

// Gallery
/*function gallery(id) {
	var gallery = $(id);
	var bigImg = $(id+" .big img");
	$(id+" .preview a").click(function() {
			var a = $(this);
			bigImg.attr('src', this.href);
			a.parent().find('a').removeClass('active');
			a.addClass('active');
			return false;
	});
}

function tabs(settings) {
	var tabsWrap = settings;

	//Hide all tabs except the first one
	$(tabsWrap+" .tabs:gt(0)").hide();
	//and activate the first tab tag
	$(tabsWrap+" .tabtags a:first").addClass("active");
	
	$(tabsWrap+" .tabtags a").click(function(){
		// Hide other tabs
		$(tabsWrap+" .tabs").hide();
		// Show requested
		var a = $(this);
		var target = a.attr("href").substr(1);
		$(tabsWrap+" #"+target+".tabs").show();
		//Reset active tab handler
		$(tabsWrap+" .tabtags a").removeClass("active");
		//Set new active tab handler
		a.addClass("active");
	
		return false;
	});
 }*/

 // Ikar carousel on the home page as object
function Carousel(carousel) {
	// Initial settings
	var cal = $(carousel);
	var reel = cal.find('.reel');
	var nav =  $('#nav'); //$('.subnav').filter('.active');
	var items = nav.find(' > ul li');
	var links = items.find('a.sec');
	var xhr_links = $('a.xhr');
	var sub_xhr_links = $('a.sub_xhr');
	var has_submenu = items.find('a.has_submenu');
	
	//How much frames are hidden;
	var limit = (reel.find('.frame').length-1)*100;
	
	//Type of easing
	var style = 'easeInOutQuart'
	
	// If there's no any carousel - give up
	if (!$('#carousel')) { return false; }
	
	// Get the index of the active item
	var default_index = items.index(nav.find('.active').parent());
	
	// Define if SWFAddressEvent.CHANGE happened because user cliked on a link or loaded new URL
	var change_happened_onclick = false;
	
	
	items.find(' > a:not(.contacts-link)').click(function(e) {
		//Close all submenues
		_hideGreenModal($(this), '#contacts-modal');
		
		
		
		// Prevent default action for links in the main menu
		return false;
	});
	
	
	// When user switches frame...
	// This will trigger SWFAddressEvent.CHANGE event in case swfaddress is used
	links.click(function(e) {
		// Param for SWFAddress.onChange function
		change_happened_onclick = true;
		
		var clickedLink = $(this);
		var index = items.index(clickedLink.parent()); // This is the index of a list item in a list that corresponds to the index of a frame in a reel
		
		_activateLinkScrollReelChangeAddress(clickedLink, index);
		
		// Set the class of the body  to be able to style common page elements
		$('body').attr('class', clickedLink.attr('rel').substr(1));
		
		// Preventing default action form the links
		return false;
	});
	
	
	
	$('#main_home').click( function(){
		$(".reel > div:not(#home-section)").empty();
	});
	
	
	//Return content via AJAX
	xhr_links.click(function(e) {
		var link = $(this);
		
		// Load into '.frame'
		var destination = $(".reel #"+link.attr('rel').substr(1)); // remove leading slash
		
		$(".reel > div:not(#"+link.attr('rel').substr(1)+")").empty();
		
		destination.empty();
		destination.load(link.attr('href')/*, function(){
			// Refresh the set of xhr links if there's any in arrived html code
			sub_xhr_links = $('a.sub_xhr');
		}*/);
		
		return false;
	});
	
	
	//Return content via AJAX
	sub_xhr_links.click(function(e) {
		var a = $(this);
		
		// Load to the inner block of the '.text' block
		var destination = $(".reel #"+a.attr('rel').substr(1)+" #text_mid"); // remove leading slash
		destination.empty();
		destination.load(a.attr('href'));
		
		return false;
	});
	
	

	
	
	//Handle CONTACTS LINK in a special way
	 nav.find('a.contacts-link').click(function(e) {
		_showGreenModal($(this), '#contacts-modal');
		return false;
	});
	 
	// CONTACS LINK in the header
	 $('.contacts .contacts-link').click(function(e) {
		_showGreenModal($(this), '#contacts-modal');
		return false;
	});
	 
	 // NEWS ARCHIVE LINK under the news list in the home section
	 $('#home-section .news .news-arhive-link').click(function(e) {
		_showGreenModal($(this), '#news-modal');
		return false;
	});
	
	
	//Handle SERVICES LINK in a special way
	/*nav.find('a.services-link').click(function(e) {
		// Param for SWFAddress.onChange function
		change_happened_onclick = true;
		
		var clickedLink = $(this);
		var index = items.index(nav.find('a.products-link').parent()); // This is the index of a list item in a list that corresponds to the index of a frame in a reel
		
		_activateLinkScrollReelChangeAddress(clickedLink, index);
		
		// Preventing default action form the links
		return false;
	});*/
	 
	 
	this.initCarouselNav = function() {
		$('#carousel .viewport a.prev').click(function() {
			if (parseInt(reel.css('left')) < 0) {
				reel.stop(false, true).animate({'left': parseInt(reel.css('left'))+100+'%'}, {duration:700, easing: style});
				_changeActiveLink(-1);
			}
			return false;
		});
		
		$('#carousel .viewport a.next').click(function() {
			if (parseInt(reel.css('left')) > -limit) {
				reel.stop(false, true).animate({'left': parseInt(reel.css('left'))-100+'%'}, {duration:700, easing: style});
				_changeActiveLink(1);
			}
			return false;
		});
		
		
		// HELPER FUNCTIONS
	
		//Change active link
		// Get the collection of anchors (<a>) located in list items (<li>)
		// and pass class 'active' to the appropriate sibling
		var _changeActiveLink = function(dir) {
			var i = parseInt(links.index(links.filter('.active')) + dir);
			var lnk = $(links[i]);
			links.removeClass('active');
			lnk.addClass('active');
			
			// Param for SWFAddress.onChange function
			change_happened_onclick = true;
			
			// Update address bar
			SWFAddress.setValue(lnk.attr('rel'));
			// Update browser title
			
		}
	}
	
	
	var _toggleSubMenuOnPageEnter = function(rel) {
		var a = nav.find('a[rel='+rel+'][class*=has_submenu]');
		
		if (a.length > 0) {
			nav.find('.submenu').slideUp(600, style);
			a.siblings('ul').slideDown(600, style);
		} else {
			nav.find('.submenu').slideUp(600, style);
		}
	}
	
	
	var _activateLinkScrollReelChangeAddress = function(_clickedLink, _index) {
		var clickedLink = _clickedLink;
		var index = _index;
		
		// Set current link active
		links.removeClass('active');
		clickedLink.addClass('active');
		
		// Show respective frame (scroll reel)
		reel.stop(false, true).animate({left: -(index*100)+'%'}, {duration: 800, easing: style});
		
		// Update address bar
		SWFAddress.setValue(clickedLink.attr('rel'));
		// Update browser title
		
	}
	
	
	// Pass an anchor that toggles contacts modal dialog as firts parameter
	// Pass the jQuery identificator as second parameter: '#my-window'
	var _showGreenModal = function(_a, _modal) {
		var a = _a;
		var modal = $(_modal);
		
		//If this modal window was not loaded prevously
		if (modal.length < 1) {
			//Show green modal window using AJAX
			$(".viewport .ajaxgreen").load(a.attr('href')); // remove leading slash
		} else {
			if (modal.css('display')=='none') {
				// Just show it
				modal.fadeIn(200);
			} else {
				//Or hide, if it's visible
				modal.fadeOut(200);
			}
		}
	}
	
	var _hideGreenModal = function(_a, _modal) {
		var a = _a;
		var modal = $(_modal);
		
		//If this modal window was not loaded prevously
		if (modal.length < 1) {
			//Show green modal window using AJAX
			$(".viewport .ajaxgreen").load(a.attr('href')); // remove leading slash
		} else {
			if (modal.css('display')=='none') {
				// Just show it
				modal.fadeOut(200);
			} else {
				//Or hide, if it's visible
				modal.fadeOut(200);
			}
		}
	}
	
	// Change happenes when user changes url in the address bar and loads that url
	// When that happens - update title of a browser window
	SWFAddress.onChange = function() {
		// Get the deep link path
		var swfvalue = SWFAddress.getValue();
		swfvalue = (swfvalue.indexOf('/') != -1) ? (swfvalue) : ('/'+swfvalue);
		
		/*var value = SWFAddress.getValue();
		var path = SWFAddress.getPath();
		var pathNames = SWFAddress.getPathNames();
		var queryString = SWFAddress.getQueryString();
		
		console.log('swfvalue: '+swfvalue+'; value: '+value+'; path: '+path+'; path names: '+pathNames+'; query string: '+queryString);
		console.log('On Click? '+change_happened_onclick);*/
		
		// Set main menu class (style)
		nav.attr('class', (swfvalue=='/'?'home-section':swfvalue.substr(1))+'-menu');
		
		_toggleSubMenuOnPageEnter(swfvalue);
		
		// Act only if the change didn't happen because the user clicked on a link
		if (!change_happened_onclick) {
			// Get the carousel navigation link
			var lnk = links.filter('[rel="'+swfvalue+'"]');
			
			/*console.log('158: Links: '+links.length);
			console.log('158: Lnk: '+lnk.length);*/
			
			// If the deepLink path is not empty
			if (swfvalue != '/' && lnk.length > 0) {
				/*console.log('162');*/
				
				// Set it active
				links.removeClass('active');
				lnk.addClass('active');
				
				// Show proper frame
				reel.stop(false, true).animate({left: -(items.index(lnk.parent())*100)+'%'}, {duration: 800, easing: style});
				
				//Do AJAX
				$(".reel #"+swfvalue.substr(1)).load(swfvalue.substr(1)+'.html'); // remove leading slash
			} else {
				/*console.log('171');*/
				
				// Set proper reel navigation link active
				links.removeClass('active');
				$(links[default_index]).addClass('active');
				// Scroll reel to the proper position
				reel.stop(false, true).animate({left: -(default_index*100)+'%'}, {duration: 1000, easing: style});
			}
			
			// Update browser title
			
		} else {
			// Reset this param
			change_happened_onclick = false;
		}
	}
} // ikarCarousel
 
jQuery(function($){
		// Zebra table
		$('table.data tr:even').addClass('even');
		
		//$('#product .tabs li').addClass('even');
		
		//Tell main menu that JS is available
		$('#nav ul').addClass('js');
});

