$(function(){
  $("ul li:first-child").addClass("firstchild");
  $("ul li:last-child").addClass("lastchild");

});


hover = function() {
	var sfEls = document.getElementById("header").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" hover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", hover);// JavaScript Document

function highlight(field) {
       field.focus();
       field.select();
}

$(document).ready(function(){

	var trans = false;
	var sidebarOpen = true;
	var sidebar = $('#sidebar');
	var handle = $('.handle');
	var enquiryHolder = $('#enquiryHolder');
	var enquire = $('#enquire');
	var enquireLink = $('a.enquire');
	var enquireClose = $('a.closeform');
	var customHolder = $('#customHolder');
	var custom = $('#custom');
	var customLink = $('a.customise');
	var customClose = $('input.closecustomise');
	handle.hide();
	var feature = ($(location).attr('href').indexOf('#ecotimer') > -1) ? 'product_19' : false;

	$('#products_page_container').cycle({ 
		fx:     'fade', 
		speed:  'slow', 
		timeout: 12000, 
		prev:   '.leftarrow', 
		next:   '.rightarrow',
		prevNextClick: function () {
			enquire.animate({bottom: '-' + (parseInt(enquire.height())+20)}, 400, function() { enquiryHolder.css('height', '1px'); });
		},
		before: function() {	
			if(trans == false) {
				trans = true;	
			} else if (trans == true) {
				if(sidebarOpen) {
					sidebar.animate({left:'-' + sidebar.width()}, 400);
					handle.fadeIn(200);
					sidebarOpen = false;
				}
			}
		},
		manualMenu: '#menu-test-sidebar',
		delay: (feature) ? 200 : null,
		dynamicFirst: (feature) ? feature : null
	});
	$.cookie("feature", null)
	
	function sideCatMenu(ind, el) {
		return '<div class="service' + el + '"></div>';	
	}	
	
	$('#11').cycle({ 
		fx:     'fade', 
		speed:  'slow', 
		timeout: 2000, 
		pager:  '#11',
		pagerAnchorBuilder: function(idx, slide) { 
			// return selector string for existing anchor 
			return '#homeimagenav li:eq(' + idx + ') a'; 
		} 
	
	});
	
	sidebar.mouseleave(function(e) {
		sidebar.animate({left:'-' + sidebar.width()}, 400);
		handle.fadeIn(200);
		sidebarOpen = false;
	});
	
	handle.mouseover(function() {
			sidebar.animate({left: 0}, 400);
			handle.fadeOut(200);
	});
	
	
	enquireLink.click(function(e) {
		e.preventDefault();
		enquiryHolder.css('height', (parseInt(enquire.height())+20));
		enquire.animate({bottom: 0}, 400);
	});
	
	enquireClose.click(function(e) {
		e.preventDefault();
		enquire.animate({bottom: '-' + (parseInt(enquire.height())+20)}, 400, function() { enquiryHolder.css('height', '1px'); });
	});
	
	customLink.click(function(e) {
		e.preventDefault();
		customHolder.css('height', (parseInt(custom.height())+20));
		custom.animate({bottom: 0}, 400);
	});
	
	customClose.click(function(e) {
		e.preventDefault();
		custom.animate({bottom: '-' + (parseInt(custom.height())+20)}, 400, function() { customHolder.css('height', '1px'); });
	});
	
	$('.hover-state-menu ul').children().each(function(index) {
		if(!$(this).hasClass('current-menu-item')) {
			//$(this).animate({opacity: 0},10);
			$(this).find('a').hide();
		}
	});

	$('.hover-state-menu ul li').mouseover(function() {
			if(!$(this).hasClass('current-menu-item')) {
				//$(this).animate({opacity: 1},200);
				$(this).find('a').fadeIn(200);
			}
	});
	$('.hover-state-menu ul li').click(function() {
		$(this).addClass('current-menu-item');
	});
	$('.hover-state-menu ul li').mouseleave(function() {
		if(!$(this).hasClass('current-menu-item')) {
			//$(this).animate({opacity: 0},50);
			$(this).find('a').fadeOut(50);
	  	}
	});
	
	//IMPORTANT:
	//The Script to close the enquiry form after succesful send is in 'plugins/contact-form-7/scripts.js' line 122:
	//$('#enquire').animate({bottom: '-' + (parseInt($('#enquire').height())+20)}, 400, function() { $('#enquiryHolder').css('height', '1px'); });
	
	var currentSubCat;
	var activeCat;
	
	$('.sub-menu').hide();
	$('#sidecats .menu-item').click(function(e) {
		if($(this).parent().hasClass('sub-menu')) {
		} else {
			if($(this).attr("id") != activeCat) {
				if(currentSubCat) {
					currentSubCat.slideUp();
				}
				currentSubCat = $(this).find('.sub-menu');
				currentSubCat.slideDown();
				activeCat = $(this).attr("id");
			}
		}
	});
	
	/*  ecotimer */
	
	$('.gotoFeature').click(function(e) {
		e.preventDefault();
		var rel = $(this).attr('rel');
		if($('#products_page_container').length > 0) {
			$('#products_page_container').cycle.override(rel);
			window.location.hash = 'ecotimer';
		} else {
			window.location.href = "http://dcshort.com/#ecotimer";	
		}
	});
	
});
