function getCookie(name) {
    if (document.cookie.length>0) {
        start = document.cookie.indexOf(name + "=");
        if (start != -1) {
            start = start + name.length + 1;
            end = document.cookie.indexOf(";",start);
            if (end == -1) {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(start,end));
        }
    }
    return "";
}

function setCookie(name, value, trigger) {
    var current_date = new Date();
    var expiry_date = new Date();
    var expires = current_date.getTime();
    if ((typeof(trigger) != 'undefined') && (trigger == 'removed')) {
        expires += 3600*24000;
    }
    else {
        expires += 3600*2000;
    }
    expiry_date.setTime(expires);
    document.cookie = name + "=" + escape(value) + ((expiry_date==null)?"":";expires=" + expiry_date.toGMTString()) + ";path=/";
}

jQuery(document).ready(function($){

    $('.directory_login a').fancybox();
    var host = window.location.hostname;

    if($('#advertise-heading').length > 0) {
        if (host != "directory.drive.com.au" && host != "directory.essentialbaby.com.au" && host != "directory.domain.com.au") {
          var signupSidebar = $('.request-info-container2');
          var signupSidebarPosition = signupSidebar.position();
          var sidebarHeight = $('#sidebar').height();
          var contentHeight = $('#content-container').height();
          var contentTop = 200;

          $(window).scroll(function(){
              var top;
              var scroll = $(window).scrollTop();

              if(scroll < contentTop) {
                  /* Stop when we hit the top. */
                  top = 0;
              } else if (scroll > (contentTop + contentHeight - sidebarHeight)) {
                  /* Stop when we hit the bottom of the content container. */
                  top = contentHeight - sidebarHeight;
              } else {
                  /* Follow the scroll */
                  top = scroll - signupSidebarPosition.top;
              }

              signupSidebar.stop().css({"position": "relative"}).animate({"top": top + "px"});
          });
        }
    }

    if($('#omg-video-img').length > 0) {
      $('#omg-video-img').fancybox();
    }

	$(window).load(function() {
		$('#business-slider').nivoSlider({
			effect: 'boxRainGrow', // Specify sets like: 'fold,fade,sliceDown'
			pauseTime: 6000 // How long each slide will show
		});
	});

	/* ================ */
	/* global JS object */
	/* ================ */
	var omgGlobalJS = function() {
		return{
			/************************
				Init function
				-- attach event handlers
				-- fire drop down menu if present
				-- country select disable for forms
			************************/
			init: function(){
				// drop down menus if LI has UL menu will fire
				if ($("#navigation-items") != null) {
					$("#navigation-items").children("li").hover(
						function(){
							$(this).children("ul").show();
							if($(this).children("ul").children("li").children("ul")){
								$(this).children("ul").children("li").hover(
									function(){
										$(this).children("ul").show();
									},
									function(){
										$(this).children("ul").hide();
									}
								);
							}
						},
						function(){
							$(this).children("ul").hide();
						});
				}
				// end drop down
				
				//network nav handler
				if ($("#omg-network-nav-btn") != null) {
					$("#omg-network-nav-btn").click(this.networkNav);
				}
				
				//generic contact form country handler Aus + Nz
				if ($("#cf-country") != null) {
					$("#cf-country").change(function () {
						  $("#cf-country option:selected").each(function () {
							  var stateTxt = $(this).val();
								if(stateTxt == "Australia"){
									$("#cf-state").removeAttr("disabled");
								} else{
									$("#cf-state").attr("disabled", true); 
								}
							  });
						}).trigger('change');
				}
				//generic contact form handler
				if ($(".cf-submit") != null) {
					//$(".cf-submit").click(this.contactForm);
				}
			},
			// end init
			/************************
				network nav open close
				-- fire on link click
				-- slide open panel
				-- switch open/close arrow class
			
			************************/
			networkNav : function(e){
				e.preventDefault();
				if($(this).hasClass("btn-open")){
					$(this).removeClass("btn-open");
					$(this).addClass("btn-closed");
				}else{
					$(this).removeClass("btn-closed");
					$(this).addClass("btn-open");				
				}
				$("#omg-network-nav ul").slideToggle("normal");
			}
		 }
	}();

	/* generic get cookie lookup */
	function getCookie(name) {
		if (document.cookie.length>0) {
			start = document.cookie.indexOf(name + "=");
			if (start != -1) {
				start = start + name.length + 1;
				end = document.cookie.indexOf(";",start);
				if (end == -1) {
					end = document.cookie.length;
				}
				return unescape(document.cookie.substring(start,end));
			}
		}
		return "";
	}

	/* generic set cookie */
	function setCookie(name, value, expiryday) {
		var expirydate = new Date();
		expirydate.setDate(expirydate.getDate() + expiryday);
		document.cookie = name + "=" + escape(value) + ((expirydate==null)?"":";expires=" + expirydate.toGMTString());
	}

	omgGlobalJS.init();

	/* find by map button */	
	$(".find-by-map .button").bind("click", function() {
		$(".find-by-map .map").toggle("fast");
	});
	
	$("form[name*=form_search]").each(function(){
		var $form = $(this);
		var $inp_category = $(":input[name*=text_47]", $form);
		var $inp_location = $(":input[name*=text_57]", $form);
		var $btn_submit = $(":submit[name*=submit]", $form);
		
		var $init_cat_val = $.trim($inp_category.val());
		var $init_loc_val = $.trim($inp_location.val());
	});


  $('.featured-container').featured();
	

  if(typeof defaultCategories == 'undefined') {
    defaultCategories = [];
  }

	// do autocomplete (suggested text)
	$(":input[class*=predictive_category]").suggestion({
		url: '/static/predictive/category.php',
		dataType: 'json',
		text: 'Type to search for category',
    defaults: defaultCategories,
    defaultText: 'Popular Categories'
	});
	
	$(":input[class*=predictive_location]").suggestion({
		url: '/static/predictive/search.php',
		dataType: 'json',
    defaultUrl: '/static/predictive/search.php?default=true',
		text: 'Type to search for location'
	});
	
	/* get quotes - cat selector */
	$("#quote_form.categories li span").click(function() {
		var checkbox = $(this).parent().children("input:checkbox");
		if (checkbox.attr("checked")) {
			checkbox.attr('checked', false);
		}
		else {
			checkbox.attr('checked', true);
		}
	}).hover(function() {
		$(this).css("font-weight", "bold");
	}, function() {
		$(this).css("font-weight", "normal");
	});
	
    if($('.yahoo_ads').length > 0) {
        // quotes suck in the adds
            $('.yahoo_ads').append($('#listings').html());
            $('#listings').html('');
            $('.yahoo_ads small').append('<div style="clear:both;margin-bottom:10px"></div>');
                
            $('.yahoo_ads').append('<div style="clear:both;margin-bottom:10px"></div><hr />');
    }
    
});

function adfeed_loadfeed(nmc_id) {
    var url = document.location.href;
    if(url.indexOf('?') != -1) {
        url += '&nmc=' + nmc_id;
    } else {
        url += '?nmc=' + nmc_id;
    }
    
    $('#ad-nmc-'+nmc_id).load(url, function() {
        if(typeof swap_cpc_listings == 'function') {
            swap_cpc_listings('ad-nmc-'+nmc_id);
        }
    });        
}

