//functions to be executed on page load
$(function(){
	
	//Open all links with rel="external" in new window
	$(function(){
		$('a').filter(function(){
			return (!this.target && (this.href.indexOf(window.location.hostname) == -1 || this.href.match(/\.pdf$/i)));
		}).attr('target', '_blank');
	});
	


	//Clear value in search input box
	swapValues = [];
	$(".f-search input,.comment-input input,.comment-text input,.comment-text textarea").each(function(i){
		swapValues[i] = $(this).val();
		$(this).focus(function(){
			if ($(this).val() == swapValues[i]) {
				$(this).val("");
			}
		}).blur(function(){
			if ($.trim($(this).val()) == "") {
				$(this).val(swapValues[i]);
			}
		});
	});
	
	if($("#dropdown").length) {		
	  $("#primary-nav li#traffic-solutions a,#dropdown").hover(function(){ 
		$("#dropdown").css("display","block");
		$("#primary-nav li#traffic-solutions a").addClass("current");
		$("#search,#map").css("z-index","-1");
	  }, function() {
		$("#dropdown").hide();  
		$("#primary-nav li#traffic-solutions a").removeClass("current");
		$("#search,#map").css("z-index","0");
	  });
	}
	
		$("ul.latest-news li").last().css("background","none");
	
	if($(".info").length) {
		$(".tooltip, .info a.icon").hover(function() {
			$(".tooltip").show();
			$(".info a.icon").addClass("icon-hover");
		},function(){
			$(".tooltip").hide();
			$(".info a.icon").removeClass("icon-hover");
		});
	}
	
	if($(".photo-gallery").length) {
		$("a[rel='gallery-group']").colorbox();
		$("a[rel='gallery-group']").click(function() {
			$("#colorbox").addClass("gallery-box");										   
		});
	}

	$("#colorbox #cboxClose").click(function() {
		$("#colorbox").removeClass("gallery-box");										 
	});
	
	if ($(".video-guides").length) {
		$(".video-thumb a").click(function() {
			$("#colorbox").addClass("gallery-box");								   
		});
	}
	
	$(".search-submit").click(function(){ 
		window.location = "search-results.htm";
	});
	
	$(".f-search input").keypress(function(e) {
		if (e.which == 13) {
			window.location = "search-results.htm";
		}
	});	
	
});


//functions to be executed inline
var mylib =
{
	color_box :
	{
		init : function()
		{
			$(function() {
  				$(".colorbox").colorbox({width:"472px", height:"466px", iframe:true, scrolling:false});
				$(".colorbox").click(function() {
					$("#colorbox").removeClass("gallery-box");
				});
			});
		}
	},
	video_box :
	{
		init : function() {
			$(function() {
				$(".video-thumb a").colorbox({
					width:654, 
					height:454, 
					iframe:true, 
					scrolling:false
				});
				var platform = navigator.platform.toLowerCase();
				if (platform == "macintel"){
					$(".colorbox").colorbox({width:"15%", height:"67%", iframe:true, scrolling:false});
				}				
			});
		}
	},
	payment_systems_process :
	{
		init : function()
		{
			$(function(){
				$(".optional-extras li a").click(function() {
					var clicked = $(this).parent().attr("id");
					$(".optional-extras li#"+clicked).toggleClass("hide");
					$(".process-graphics ."+clicked).fadeToggle(250);
					return false;
				});
			});
		}
	},
	gmap :
	{
		init : function(){
		$(function () {

                    $("#map").gMap({ markers: [{ latitude: 54.637227923948104, longitude: -5.67378316955568,
                        html: "<strong>Advanced Parking Solutions</strong><br />21B Enterprise Road<br />Bangor BT19 7TA",
                        popup: false
                        }],								   
                        icon: { image: "images/marker.png",
                            iconsize: [40, 49],
                            iconanchor: [20, 49],
                            infowindowanchor: [20, 49]
                        },
						controls: ["GSmallMapControl"],						
                        zoom: 8
                    });
                });	
		}
	},
	tender_spec_tabs :
	{
	 	init : function() {
			$(function() {
				$(".tender-documents,.client-tab").hide();
				$(".tender-documents,.client-tab").first().fadeIn(250);
				$("#tab-nav li a").first().addClass("current");
				$("#tab-nav li a").click(function() {
					$("#tab-nav li a").removeClass("current");
					$(this).addClass("current");		
					var clicked = $(this).attr("href");
					$(".tender-documents,.client-tab").hide();
					$(clicked).fadeIn(250);
					return false;
				});
			});	
		}
	},
	announcement_fade :
	{
		init : function() {
			$(function() {
				var items = $("#list-fade li").length,itemLi = $("#list-fade li"),number = 1,currentID,nextID;
				itemLi.css("position","absolute");
				itemLi.hide();
				itemLi.first().addClass("current-item");
				itemLi.first().fadeIn(550);
				itemLi.each(function() {
					$(this).attr("id","item"+number);	
					number++;
				});
				
				var fadeItems = function() {
					currentID = $(".current-item").attr("id");											   
					$("#list-fade li#"+currentID).fadeOut(550).removeClass("current-item");
					nextID = currentID.slice(4);
					if (nextID < items) {
					nextID = parseInt(nextID) + 1;
					}
					else {
						nextID = 1;	
					}
					$("#item"+nextID).fadeIn(550).addClass("current-item");						
				}
					
				timer = setInterval(fadeItems,4000)
			});
		}
	}
}
