// JavaScript Document




// read the theme cookie
var selected_theme = $.cookie('theme_cookie2');

//alert(selected_theme);

if ( selected_theme != null )
{
	// initiate the theme function
	set_theme(selected_theme);	
}
else
{
	set_theme('_1');
}

jQuery(document).ready(function() {
	
	
	// start the clock
	jQuery(".jclock").jclock();
	
	// date
	get_date();
	
	

	
	// ALL PAGES - SHOW or HIDE the skyscraper depending on browser width
	var MYnewWindowWidth = jQuery(window).width();
	if ( MYnewWindowWidth > '1200' )
	{
		jQuery("#askyscraper").show();
	}
	else
	{
		jQuery("#skyscraper").hide();
	}
	// ALL PAGES - SHOW or HIDE the skyscraper depending on browser width when the page is resized
	jQuery(window).bind("resize", resizeWindow);
	function resizeWindow(e)
	{
		var newWindowWidth = jQuery(window).width();
		if ( newWindowWidth < '1200' )
		{
			jQuery("#skyscraper").hide();
		}
		else
		{
			jQuery("#skyscraper").show();
		}
	}
	

	jQuery(function() {
		jQuery("#flowpanes").scrollable({circular: true, mousehweel: true}).navigator();
	});
	
	// rollover for button elements
	jQuery(".btn_blue").hover(
  		function () {
    		jQuery(this).addClass("btn_blue_hover");
  		}, 
  		function () {
    		jQuery(this).removeClass("btn_blue_hover");
  		}
	);
	
	jQuery(".btn_blue2").hover(
  		function () {
    		jQuery(this).addClass("btn_blue_hover2");
  		}, 
  		function () {
    		jQuery(this).removeClass("btn_blue_hover2");
  		}
	);
	
	jQuery(".btn_grey").hover(
  		function () {
    		jQuery(this).addClass("btn_grey_hover");
  		}, 
  		function () {
    		jQuery(this).removeClass("btn_grey_hover");
  		}
	);
	
	
	// show or hide nav drop down menus - nav_list_open
	jQuery("#nav_ul > li").bind('mouseenter', nav_list_open);
//	jQuery("#nav").bind('mouseleave', nav_list_timer);
	jQuery("#nav").bind('mouseleave', nav_list_close);
	nav_list_close
	
	// nav intro text & video tabs
	jQuery(".intro_tabs").tabs(".intro_panes > .intro_pane");
	
	// nav item arrows
	jQuery(".has_dropdown").hover(
  		function () {
			jQuery(this).find("img").attr("src", "/wp-content/themes/bigon/images/arrow_down_white.png");
  		}, 
  		function () {
			jQuery(this).find("img").attr("src", "/wp-content/themes/bigon/images/arrow_down_black.png");
  		}
	);	
	
	
	// nav bar - calculate width of theme selector
	var nav_width = 0;
	jQuery('#nav_ul').each(function() {
		nav_width += jQuery(this).outerWidth(true);
	});
	var nav_width_2 = nav_width - 20;
	var nav_last_width = 980 - nav_width_2;
	jQuery("#nav_ul a:last").css("width", nav_last_width);
	//alert(set_nav_last_width);
	
	
	
	
	
	
	
	
	// Channel menus within the main drop-drop menus
	/*	jQuery(".ddm_channel_title").hover(
  			function () {
				var ddm_channel_id = jQuery(this).attr("id");
				var sub_to_show = ddm_channel_id + '_channels';
				jQuery(".subs").css("display", "none");
				jQuery("#"+sub_to_show).css("display", "block");
  			}, 
  			function () {
				jQuery(".subs").css("display", "none");
  			}
	);
	*/
	
	
	// channels and sub-channels
	jQuery(".tabs_nav_dd").tabs(".subs_panes > .subs", { event: 'mouseover' } );
	
	// football channel feature area
	jQuery(".football_channel_tabs").tabs(".football_channel_panes > .football_channel_pane", { event: 'mouseover' } );
	
	// football channel tabs
	jQuery(".football_club_tabs").tabs(".football_club_panes > .football_club_pane" );
	
	// football club page image gallery
	jQuery("#flowpanes_football_club_gallery").scrollable({circular: true, mousehweel: true}).navigator();
	
	
	
	// home page feature area panes
	jQuery(".panes_item").hover(
  			function () {
    			jQuery(this).find(".panes_text").css("top", "105px");
  			}, 
  			function () {
    			jQuery(this).find(".panes_text").css("top", "151px");
  			}
	);
	
	
	
	
	
			
	
	// three boxes - remove the border from the third one in the row
	jQuery(".blue_puff_outer:nth-child(3n)").addClass("no_mr");
	
	
	
	
	
	
	// show & hide the Explore BIG ON explorer
	// first check to see if the Log In explorer is open first
	jQuery("#explore_link").click(function() {
  		
		if ( jQuery("#explorer_2").is(":hidden") )
		{
			jQuery("#explorer_1").slideToggle();
		}
		else
		{
			jQuery("#explorer_2").slideToggle();
			jQuery("#explorer_1").slideToggle();
		}
	});
	// same functionality for the close button within the explorer
	jQuery("#explorer_1 .explorer_close").click(function() {
  		
		if ( jQuery("#explorer_2").is(":hidden") )
		{
			jQuery("#explorer_1").slideToggle();
		}
		else
		{
			jQuery("#explorer_2").slideToggle();
			jQuery("#explorer_1").slideToggle();
		}
	});
	
	
	
	// show & hide the Explore Explore Log In explorer
	// first check to see if the About BIG ON explorer is open first
	jQuery("#themes_link").click(function() {
  		
		if ( $("#explorer_1").is(":hidden") )
		{
			jQuery("#explorer_2").slideToggle();
		}
		else
		{
			jQuery("#explorer_1").slideToggle();
			jQuery("#explorer_2").slideToggle();
		}
	});
	// same efunctionality for the close button within the explorer
	jQuery("#explorer_2 .explorer_close").click(function() {
  		
		if ( $("#explorer_1").is(":hidden") )
		{
			jQuery("#explorer_2").slideToggle();
		}
		else
		{
			jQuery("#explorer_1").slideToggle();
			jQuery("#explorer_2").slideToggle();
		}
	});
	
	
	
	
	
	
	
	
	
	
	// mouse enter/leave a form field
	jQuery(".mouse_enter").focus(function () {
    	jQuery(this).addClass("form_field_focus");
    });
	jQuery(".mouse_enter").blur(function () {
    	jQuery(this).removeClass("form_field_focus");
    });
	
	
	// show or hide the more articles within the news boxes
	jQuery(".news_box_more_less span").toggle(
  		function () {
    		jQuery(this).html("<a href='javascript:;'>See less articles</a>").removeClass("news_box_arrow_down").addClass("news_box_arrow_up");
			jQuery(this).parent().parent().find(".news_box_more_articles").slideToggle();
			jQuery(this).parent().find("b").css("visibility","visible");
  		}, 
  		function () {
    		jQuery(this).html("<a href='javascript:;'>See more articles</a>").removeClass("news_box_arrow_up").addClass("news_box_arrow_down");
			jQuery(this).parent().parent().find(".news_box_more_articles").slideToggle();
			jQuery(this).parent().find("b").css("visibility","hidden");
  		}
	);
	
	
	// display the first news_box in each section
	jQuery("#news_box_container_section0_1").fadeIn();
	jQuery("#news_box_container_section1_1").fadeIn();
	jQuery("#news_box_container_section2_1").fadeIn();
	jQuery("#news_box_container_section3_1").fadeIn();
	jQuery("#news_box_container_section4_1").fadeIn();
	
	
	// home page news box display code
	jQuery(".news_box_nav a").click(function() {
  			
			// highlight the correct nav item when clicked
			jQuery(this).parent().find("a").removeClass("news_box_nav_current");
			jQuery(this).addClass("news_box_nav_current");
			
			// hide the currently visible news_box_container
			jQuery(this).parent().parent().find(".news_box_container").hide();
			
			// show the relevant news_box_container
			var which_news_box = jQuery(this).attr("rel");
			jQuery("#news_box_container_"+which_news_box).fadeIn("fast");
			
		});
		
	
	// remove the margin right from every 5th image/article on sub channel pages
	jQuery(".sub_channel_news_box_images span:nth-child(5n)").addClass("no_mr");
	
	
	// accordions - set the last h2 to have no margin bottom
	jQuery("#accordion h2:last").css("marginBottom","0");
	
	// weather accordion - add bottom border to the last li
	jQuery("#weather_regions_accordion h2:last").css("borderBottom","1px solid #fff");
	
	
	
	// replies to comment on articles always have a white background
	jQuery(".reply").css("background","#fff");
	
	
	// business directory
	
	jQuery(".bd_slide .bd_cat:nth-child(3n)").addClass("no_mr");
	
	jQuery(".faded_term").focus(function () {
    	jQuery(this).val("");
    });
	
	jQuery(".bd_cat").hover(
							
					  function()
					  {
						  jQuery(this).find('.bd_cat_link').animate({top: '127px'},50);
					  },
					  function()
					  {
						  jQuery(this).find('.bd_cat_link').animate({top: '131px'},50);
					  }							
							
					);
	
	jQuery("#bd_nav span").hover(
					
					function()
					  {
						  jQuery(this).css('textDecoration', 'underline');
					  },
					  function()
					  {
						  jQuery(this).css('textDecoration', 'none');
					  }							
							
				);
	
	jQuery("#business_directory_search h2 span").toggle(
							
					  function()
					  {
						  jQuery(".inner_638_search_inner").slideDown();
						  jQuery(this).addClass("bd_min");
					  },
					  function()
					  {
						  jQuery(".inner_638_search_inner").slideUp();
						  jQuery(this).removeClass("bd_min");
					  }							
							
				);
	
	// small hover function for the Widget manager labels
	jQuery(".wm_row label").hover(
							
					  function()
					  {
						  jQuery(this).addClass("bold");
					  },
					  function()
					  {
						  jQuery(this).removeClass("bold");
					  }							
							
				);
	
	
	// Widget Manager - See to see how many checkboxes are checked
	jQuery("#wm_top_row input:checkbox").click(		
					  function()
					  {
						  var how_many = jQuery("#wm_top_row input:checkbox:checked").length;
						  if ( how_many > 3 )
						  {
							  alert("You may only select three Widgets per row.");
							  return false;
						  }
					  }
					);
	jQuery("#wm_top_row label").click(		
					  function()
					  {
						  var how_many = jQuery("#wm_top_row input:checkbox:checked").length;
						  if ( how_many > 3 )
						  {
							  alert("You may only select three Widgets per row.");
							  return false;
						  }
					  }
					);
	jQuery("#wm_bottom_row input:checkbox").click(		
					  function()
					  {
						  var how_many = jQuery("#wm_bottom_row input:checkbox:checked").length;
						  if ( how_many > 3 )
						  {
							  alert("You may only select three Widgets per row.");
							  return false;
						  }
					  }
					);
	jQuery("#wm_bottom_row label").click(		
					  function()
					  {
						  var how_many = jQuery("#wm_bottom_row input:checkbox:checked").length;
						  if ( how_many > 3 )
						  {
							  alert("You may only select three Widgets per row.");
							  return false;
						  }
					  }
					);
	
	
	// Clear the More Cities field when the user clicks into it
	jQuery("#more_cities").focus(function () {
    	var currentVal = $(this).val();
		if ( currentVal == "More Cities...")
		{
			$(this).val("");
		}
    });
	// Populate the More Cities text field if the user clicks out it and it's empty
	jQuery("#more_cities").blur(function () {
    	var currentVal = $(this).val();
		if ( currentVal == "")
		{
			$(this).val("More Cities...");
		}
    });
	
	
	// site colour themes
	jQuery(".themes span").hover(
							
					  function()
					  {
						  jQuery(this).addClass("hover_theme");
					  },
					  function()
					  {
						  jQuery(this).removeClass("hover_theme");
					  }							
							
				);
	
	
	

	
	// find which theme button has been clicked and pass that value to the "set_theme" function
	jQuery(".themes span").click(		
					  function()
					  {
						  // initiate the theme function
						  var selected_theme = jQuery(this).attr("rel");
						  set_theme(selected_theme);
						  
						  // create a cookie holding the value of which button has been clicked
						  $.cookie('theme_cookie2', selected_theme, {path: '/'});
				      }
					);
	
	
	// open the footer contact us form
	jQuery("#footer_contact").click(		
					  function()
					  {
						  // hide the button
						  jQuery(this).css("visibility", "hidden");
						  
						  // show the form
						  jQuery("#footer_top_form").slideToggle();
						  
						  // stop the page reloading
						  return false;
						  
				      }
					);
	
	// close the footer contact us form
	jQuery("#footer_top_form .explorer_close").click(		
					  function()
					  {
						  // show the form
						  jQuery("#footer_top_form").slideToggle();
						  
						  // hide the button
						  jQuery("#footer_contact").css("visibility", "visible");
						    
						  // stop the page reloading
						  return false;
						  
				      }
					);
	
	
	// add the main background image onto the page
	jQuery("body").css("backgroundImage","url(/wp-content/themes/bigon/images/bg_glasgow.jpg)");
	
	// advertise with us page
	jQuery(".display_ads a[rel]").each(function() {
		$name = $(this).attr('name');
		if($name.substring(1,0) == '#') {
			$(this).overlay({target: $name});
		}
	});
	
	
});



// nav menu code
// Sourced from: http://javascript-array.com/scripts/jquery_simple_drop_down_menu/

var timeout    = 200;
var closetimer = 0;
var ddmenuitem = 0;

function nav_list_open()
{
	jQuery("#nav").css("height", "398px");
	
	nav_list_canceltimer();
	var nav_id = $(this).attr("id");
	var drop_down_to_display = nav_id + "_dropdown";
	if (!jQuery(this).hasClass('has_dropdown')) {
            nav_list_timer();
        } else {
            
			jQuery('.dropdown').hide();
			$("#"+drop_down_to_display).show();            

			if (jQuery('#nav_dropdown_outer').css('top') != 40 && !jQuery('#nav_dropdown_outer').hasClass('opening'))
            {
                jQuery('#nav_dropdown_outer').stop().show().addClass('opening')
				.animate({top: "40px"}, 600, 'swing', function() {
                // Animation complete.
			 		jQuery('#nav_dropdown_outer').removeClass('opening').removeClass('closing');
              });
            }  
        }

}

function nav_list_close()
{
    jQuery('#nav_dropdown_outer').stop().removeClass('opening').addClass('closing')
	.animate({top:-358}, 600, 'swing', function () {
		jQuery(this).hide();
		jQuery("#nav").css("height", "40px");
		jQuery('#nav_dropdown_outer').removeClass('opening');
    }); 
}

function nav_list_timer()
{
    closetimer = window.setTimeout(nav_list_close, timeout);
}

function nav_list_canceltimer()
{
    if(closetimer)
    {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}



// display the date on the page

function get_date()
{
	// get the current day
	var day = new Date();
	var weekday = new Array(7);
	weekday[0] = "Sun";
	weekday[1] = "Mon";
	weekday[2] = "Tue";
	weekday[3] = "Wed";
	weekday[4] = "Thu";
	weekday[5] = "Fri";
	weekday[6] = "Sat";
	
	// get the current date, month and year 
	var date = new Date();
	var month = new Array(7);
	month[0] = "Jan";
	month[1] = "Feb";
	month[2] = "Mar";
	month[3] = "Apr";
	month[4] = "May";
	month[5] = "Jun";
	month[6] = "Jul";
	month[7] = "Aug";
	month[8] = "Sep";
	month[9] = "Oct";
	month[10] = "Nov";
	month[11] = "Dec";
	var year = date.getYear();
	if (year < 2000) { year+=1900; }

	// write the above out to the correct place
	jQuery("#date").text( weekday[day.getDay()] + " " + date.getDate() + " " + month[date.getMonth()] + ", " + year );
}




function set_theme(selected_theme)
{
	// remove the white border from all theme buttons
	jQuery(".themes span").removeClass("active_theme");
	
	// add a white border to the clicked theme button
	var selected_theme_button = 'theme' + selected_theme;
	jQuery('.'+selected_theme_button).addClass("active_theme");
							  
	//change the header and footer background image
	var new_theme = '/wp-content/themes/bigon/images/theme' + selected_theme + '.png';
	jQuery("#header").css('backgroundImage','url(' + new_theme +')');
	jQuery("#footer").css('backgroundImage','url(' + new_theme +')');
	
	//change the header background colours in the footer
	var footer_h3_bg = 'footer_header' + selected_theme;
	jQuery("#footer_sections h3").removeClass();
	jQuery("#footer_sections h3").addClass(footer_h3_bg);
	
	//change the header background colours in the Explorer
	jQuery(".explorer_outer h2").removeClass();
	jQuery(".explorer_outer h2").addClass(footer_h3_bg);
	
	//change the header background colours in the dropdown menus
	jQuery(".dropdown h2").removeClass();
	jQuery(".dropdown h2").addClass(footer_h3_bg);
	
	// change the background colour of the very bottom footer strip
	jQuery("#footer2").removeClass();
	jQuery("#footer2").addClass(footer_h3_bg);
	
	// change the background colour of the home page feature area panes
	jQuery("#hp_feature_area").removeClass();
	jQuery("#hp_feature_area").addClass(footer_h3_bg);
	
	// change the background colour of the channel page top story h1
	jQuery(".top_story h1").removeClass().addClass("white_link");
	jQuery(".top_story h1").addClass(footer_h3_bg);
	
	// change the background colour of the channel page top story related article/most read/most commented tab
	jQuery("#top_story_inner").removeClass();
	jQuery("#top_story_inner").addClass(footer_h3_bg);
	
	// change the background colour & border colour of article page comments boxes
	jQuery("#comments_container").removeClass();
	jQuery("#comments_container").addClass(footer_h3_bg);
	
	// change the background colour & border colour of nav dropdown intro text bits
	var theme_helper = 'theme_helper' + selected_theme;
	jQuery("#nav").removeClass();
	jQuery("#nav").addClass(theme_helper);
	
	// change the background colour of business directory search results tabs
	jQuery("#bd_search_results").removeClass();
	jQuery("#bd_search_results").addClass(theme_helper);
	
}

function handleLogin() {
	$.post('/wp-content/plugins/bgwp-platform/Ajax.php',
		{
			action: 'login',
			username: $('#username').val(),
			password: $('#password').val()
		},
		function(data)
		{
			if(data != "Login Successful") {
				$('#login_box_error').html('Login Failed: ' + data);
			} else {
				$('#login_box').html('You are now logged in :)');
			}
			$('#login_box_error').slideDown();
		}
	);
}
