var pageMenuIdMap = {
	'companyPage' 		: 'menuSection1',
	'equipmentPage'		: 'menuSection2',
	'servicePage'		: 'menuSection3',
	'partnershipPage'	: 'menuSection4',
	'contactsPage'		: 'menuSection5',
};
var initHoverSaturate = function(selector) {
	$(selector).hover(function(){
		var thumbOver = $(this).find("img").attr("src");
		$(this).find("a.carouselThumb").css({'background':'url(' + thumbOver + ') no-repeat center bottom'});
		$(this).find("span").stop().fadeTo('normal',0,function(){
			$(this).hide();
		});
	}, function(){
		$(this).find("span").stop().fadeTo('normal',1).show();
	});
}
var initAccordeon = function(){
	$('.accContainer').hide();
	$('.accTrigger').click(function(){
		if( $(this).next().is(':hidden') ) {
			$('.accTrigger').removeClass('active').next().slideUp();
			$(this).toggleClass('active').next().slideDown();
		}
		return false;
	});
	$('.accTrigger.active').next().show();
}
var decorateElements = function(){
	$('.footer ul.navList li:first').css('background','none');
	$('.catalogueItem:odd').css({'background':'#f8fcff'});
	$('.catalogueItem:last').css({'border':'none'});
	$('#adminPage .editTable tr:even').addClass('evenRow');
	$('.hidden').parent().prev().hide();
	var count = 0;
	$('#searchPage p').each(function(){
		var html = $(this).html().trim();
		if(html == '' || html == "&nbsp;") {
			$(this).remove();
		}
	});
	$("#searchPage p").attr('style','');
}
var highlightCurrentMenuSection = function() {
	var pageId = $('.contentContainer').attr('id');
	var menuId = pageMenuIdMap[pageId];
	if(menuId != undefined) {
		$('#'+menuId).addClass('current');
	}
}
$(document).ready(function(){
	// Initialize plugins
	$(".carouselList").jCarouselLite({
		btnNext: "#carouselRightButton",
		btnPrev: "#carouselLeftButton",
		visible: 7,
		speed: 800,
		easing: "easeInOutBack",
	});
	initHoverSaturate(".carouselList ul li");
	initAccordeon();
	decorateElements();
	highlightCurrentMenuSection();
});

function initCKEditor(rootPath) {
	var textareas = $('textarea');
	var textareaCount = textareas.length;
	for(var i=0; i<textareaCount; i++) {
		var idValue = textareas[i].id;
		CKEDITOR.replace(idValue, {
			filebrowserBrowseUrl : rootPath +'/public/js/ckeditor/pdw_file_browser/index.php?editor=ckeditor',
            filebrowserImageBrowseUrl : rootPath + '/public/js/ckeditor/pdw_file_browser/index.php?editor=ckeditor&filter=image',
            filebrowserFlashBrowseUrl : rootPath + '/public/js/ckeditor/pdw_file_browser/index.php?editor=ckeditor&filter=flash'
		});
	}
}

function setQuery(obj) {
	var searchActionUrl = obj.nextSibling.getAttribute('href');
	var query = "?query=" + obj.value;
	obj.nextSibling.setAttribute('href', searchActionUrl + query );
	return false;
}

function submitenter(theInput,e)
{
	var keycode;
	if(window.event) {
		keycode = window.event.keyCode;
	} else if(e) {
		keycode = e.which;
	} else {
		return true;
	}
	
	if (keycode == 13) {
	   setQuery(theInput);
	   window.location = theInput.nextSibling.getAttribute('href');
	   return false;
	} else {
	   return true;
	}
}
