$(function(){
	//CONTAINS ALL MAIN FUNCTIONS FOR USE BY THE DEMON WP-THEME
	//---------------------------------------------------------
	
	//FRONT PAGE CONTENT SLIDER
	//DEPENDENCIES: JQUERY, easySlider1.7.js 
	$("#slider").easySlider({
		auto: true,
		continuous: true,
		speed: 2000,
		pause: 10000
	});
	
	//-------------------------------------
	//JQUERY APPLIED CSS STYLES & CLASSES
	//-------------------------------------
	
	$("#customer_tools_list ul li:last").addClass('customer_tools_last');
	$("#customer_tools_list ul li:first").addClass('customer_tools_first');
	
	//---------------------------------------
	//FOR WEB HOSTING / VIRTUAL SERVER TABLES
	//---------------------------------------
	
	$("tr:even").css("background-color", "#DFDFDF");
	$("tr:first").css("background-color", "transparent");
	$("tr:last").css("background-color", "#FFFFFF");
	$("tr:last").css("color", "#af252b");
	$("tr:last").css("font-size", "1.3em");
	$("tr:last a").css("font-size", "0.8em");
	$("tr:last").css("padding-top", "10px");
	$('tr').each(function(){
	  $('td:first', this).css("text-align", "left");
	});
	$("tr:first td").addClass("first_level");
	$('#slider_content ul li:last').prev().addClass('help_tail_li');
	//POSITIONS OUR PNG GRAPHICS APPROPRIATELY BASED ON WIDTH OF COLUMN
	$('#options_table tr:last td').css('white-space', 'nowrap').css('padding-top', '5px');
	$("#options_table td.first_level p .unitPng").each(function() {
	var pngposition = ($(this).parents("p").width() / 2) - ($(this).width() / 2);
	$(this).css("left",pngposition);
	});
	//Generic content - add a curve to the end of the div...
	//===================================================================
	
	$('.generic_content').after('<div class="generic_content_footer"></div>');
	$('#contact_form_content').after('<div class="generic_content_footer"></div>');
	
	//---------------------
	//FOR SUBSCRIPTION FORM
	//---------------------
	
	//Content for the subscribe form...
	//===================================================================
	
	$('#sub_name').focus(function() {if($(this).val() == "yourname") {$(this).val("");}});
	$('#sub_name').blur(function() {if($(this).val() == "") {$(this).val("yourname");}});
	$('#sub_email').focus(function() {if($(this).val() == "youremail@domain.com") {$(this).val("");}});
	$('#sub_email').blur(function() {if($(this).val() == "") {$(this).val("youremail@domain.com");}});
	
	//SUBSCRIBE FORM
	//===================================================================
	
	$('#subscribe_further_trigger').click(function() {
		if ($.browser.msie && $.browser.version <= 6 ) {
			$('#subscribe_further').css("display","block");
			$('#subscribe_footer p').css("visibility","hidden");
		} else {
			$('#subscribe_further').slideToggle("fast");
			$('#subscribe_footer p').slideToggle("fast");
		};
	});
	
	//IE6 STACKING (Z-INDEX) FIX
	//THIS FUNCTION LARGELY WORKS, BUT CAN CAUSE STACKING PROBLEMS EVERY NOW AND THEN. ADD PROBLEMATIC DIVS TO THE 'NOT' STATEMENT
	//--------------------------
	
	 	var zIndexNumber = 1000;
		$('div').not('#map div, #help_slider div, .info_capsule_black, .generic_info').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
		});
		$('#help_slider, div[id^=fancybox]').css('zIndex', 1001)
	
    /*NAVIGATION MENU CODE: DEPENDENCIES JQUERY, HOVERINTENT*/
    /*------------------------------------------------------*/
    //Activate low level menus depending on where user is on mid menu...
    
    function reset_menus(){
    	$("#nav_mid li").removeClass('nav_active');
    	$("#partners_ul, #customer_ul, #business_ul").css("visibility","hidden");
    	position_menu("#nav_business","#business_ul");
    	position_menu("#nav_partners","#partners_ul");
    	position_menu("#nav_customer_support","#customer_ul");
   	}
	
	function position_menu(target_mid_tab, target_bottom_menu) {
   	if(($(target_mid_tab).position().left + $(target_bottom_menu).width()) > $("#header").width()) {
    //The menu will overlap the edge of the screen, so we need to bring it back a little.
    $(target_bottom_menu).css("left",($("#header").width()-$(target_bottom_menu).width()));
    } else {
    //we can deploy the menu into position without issue.
    $(target_bottom_menu).css("left",$(target_mid_tab).position().left);
   	}
	}
    
    $("#nav_business").mouseenter(function(){reset_menus();$(this).addClass('nav_active');$("#business_ul").css("visibility","visible");});
   	$("#nav_partners").mouseenter(function(){reset_menus();$(this).addClass('nav_active');$("#partners_ul").css("visibility","visible");});
   	$("#nav_customer_support").mouseenter(function(){reset_menus();$(this).addClass('nav_active');$("#customer_ul").css("visibility","visible");});
    
    $("#nav_bottom ul li ul li:last-child").css('border-bottom', 'none');
    
    $("#nav_bottom ul li").hoverIntent(
  	function () {
    	$(this).children('ul').slideDown('fast');
    		$(this).addClass('hover');
    		if(($(this).position().left + 202) > $("#header").width()) {
    		var overshoot = ($(this).offset().left + 202) - $("#header").width();
    		overshoot *= -1;
    		$(this).children('ul').css("margin-left",overshoot);
   	}
  	}, 
  	function () {
    $(this).children('ul').slideUp('fast');
    $(this).removeClass('hover');
  	}
	);
	
	$("#customer_tools_list").hover(function () {
    	$(this).children('ul').css('display','block');
   		// $("#customer_tools_list a:first").addClass('active');
  	}, 
  	function () {
        $(this).children('ul').css('display','none');
        // $("#customer_tools_list a:first").removeClass('active');
  	}
	);
    
    //----------------------------------------------------
    //TABBING FUNCTIONS-----------------------------------
    //----------------------------------------------------
    tabber("#tabbed_content_nav","#footer_tab_content");

	$('#main_tabbed_nav li a').each(function() {
		if ($(this).height() < 20) $(this).css("padding-top", "5px");
	});
    
    function tabber(tab_nav,tab_content) {
    	$(tab_nav + " ul li a").click(function() {
    		
            var anchor_index = $(this).parent("li").index();
			var target_content = anchor_index / 2;
        	//Tab Classes---------
       		$(this).parents('li').siblings().removeClass('active');
       		$(this).parents('li').addClass('active');
	    	$(this).parents('li').next('li').addClass('active');
	    	//--------------------
	    	//Pull up content-----
	    	//--------------------
	    	//Hide all content
			var el = $(tab_content + ' div').siblings('div').get(target_content);
	    	$(tab_content + " div").siblings('div').slideUp('slow');
	    	//Get content element and show...
	    	$(el).slideDown('slow');
	    	//Return False--------
	    	return false;
		});
   	}
    //----------------------------------------------------
  	$('#help_slider').css("visibility","visible");
  	
    $('#help_slider').tabSlideOut({
            tabHandle: '#handle',                     //class of the element that will become your tab
            tabLocation: 'right',                      //side of screen where tab lives, top, right, bottom, or left
            speed: 300,                               //speed of animation
            action: 'click',                          //options: 'click' or 'hover', action to trigger animation
            topPos: '197px',                          //position from the top/ use if tabLocation is left or right
            leftPos: '20px',                          //position from left/ use if tabLocation is bottom or top
            fixedPosition: false                      //options: true makes it stick(fixed position) on scroll
    });

    //initialise styled select boxes...
    // $('#business_type').selectbox({debug: true});
    $('#business_size').selectbox({debug: true});
    
    $("#find_it").click(function() {
		var sz = $('#business_size').val();
		if (sz == null || sz == '') return;

		var items;
		var package;
		var link;
		if (sz == '1')
		{
			items = [
				'Looking to do simple web-browsing and emailing',
				'Free migration from another broadband provider',
				'24 x 7 dedicated technical telephone support'
			];
			package = 'Home 2+';
			link = '/broadband/home-and-home-office-broadband-packages/demon-home-2';
		}
		else if (sz == 'upto5')
		{
			items = [
				'If you are looking to enable remove working and provide access in-house applications remotely',
				'Easy to set up a Virtual Private Network to securely connect to your office and seemlessley integrate with all your office systems',
				'Upgrade path for growth',
				'Reliable business grade connectivity',
				'Free business features, such as Static IP address, D-Fax, DialCompanion, Webspace',
				'Contains built-in security features to protect you and your network',
				'A sensible fair usage policy to protect you',
				'No set up fee and free pre-configured CPE to enable simple plug and play'
			];
			package = 'HomeOffice 2+';
			link = '/broadband/home-and-home-office-broadband-packages/home-and-home-office-package-details';
		}
		else if (sz == 'upto10')
		{
			items = [
				'Customers who can obtain 8Mbps download speeds with up to 1Mbps upload',
				'Update path for growth',
				'Guaranteed Performance with a Service Credit Regime',
				'Prioritised network traffic to ensure that your traffic does not slow down at peak times',
				'Free business features, such as IP address, NAT and Non-NAT options, D-Fax, DialCompanion, Webspace',
				'Dedicated UK expert telephone support 24/7',
				'No download restrictions or caps',
				'Business Broadband provides the right systems in place to enable smart and efficient collaborative working. Staff can share files, folders and calendars, plan meetings to boost productivity',
				'Looking for up to 20Mbps download speed',
				'Free setup and choice of free pre-configured router for simple installation',
				'Free migration from another broadband provider'
			];
			package = 'Business 2+';
			link = '/broadband/business-broadband/demon-business-broadband-2';
		}
		else if (sz == 'upto25')
		{
			items = [
				'Fast file transfers',
				'Video streaming',
				'Video conferencing',
				'Guaranteed Performance with a Service Credit Regime',
				'Business class internet access',
				'Prioritised network traffic to ensure that your traffic does not slow down at peak times',
				'Free business features, such as IP address, NAT and Non-NAT options, D-Fax, DialCompanion, Webspace',
				'Dedicated UK expert telephone support 24/7',
				'Access to in-house services where a static IP address is required',
				'No download restrictions or caps',
				'If you run Voice over IP',
				'Business Broadband provides the right systems in place to enable smart and efficient collaborative working. Staff can share files, folders and calendars, plan meetings to boost productivity',
				'Looking for up to 20Mbps download speed',
				'Free setup and choice of free pre-configured router for simple installation',
				'Free migration from another broadband provider'
			];
			package = 'Business 2+ Pro';
			link = '/broadband/business-broadband/demon-business-broadband-2-pro';
		}


    	$.fancybox(
			'<div id="why-package"><h2>Suggested package: <a href="' + link + '">' + package + '</a></h2><h3>Why take this package?</h3><ol><li>' + items.join('</li><li>') + '</li></ol><p><a href="' + link + '">View package</a></p></div>',
			{
				'autoDimensions' : false,
				'width'          : 350,
				'height'         : 'auto',
				'transitionIn'   : 'none',
				'transitionOut'  : 'none',
				'overlayOpacity' : 0.8,
				'centerOnScroll' : true,
				'overlayColor'   : '#000'
			}
		);
		return false;
   	});

	$('a[rel=external]').click(function() {
		window.open($(this).attr('href'), 'popup', 'toolbar=yes,location=yes,directories=yes,resizable=yes,scrollbars=yes');
		return false;
	});

    vtip();
});    