$(document).ready(function(){

	ieHover('#menu li');

	if(typeof($().galleryScroll) == 'function'){
		$('#gallery').galleryScroll({
			btPrev: 'a.link-prev',
			btNext: 'a.link-next',
			holderList: 'div',
			scrollElParent: 'ul',
			scrollEl: 'li',
			slideNum: false,
			duration : 1000,
			step: false,
			circleSlide: true,
			disableClass: 'disable',
			funcOnclick: null
		});
	}

	initSlider();

	// ---- Autofill   ----------------------------------------------------------------------------------------------------------

    $.fn.autofill = function () {
        $(this).focus(function () {
            if (this.value == this.defaultValue) {
                this.value = "";
            }
        }).blur(function () {
            if (!this.value.length) {
                this.value = this.defaultValue;
            }
        });
    };

    $("#kidspot-daily .email,#newsletter .tb, .newsletter .tb").autofill();


	// ---- Slider  ----------------------------------------------------------------------------------------------------------

    /*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
    ***/

    var currentID = $('#carousel div.wrap div.active').attr('id');
    $('#carousel .sequence a[href$=' + currentID + ']').addClass('active').parent().addClass('active');

    slideSwitch = function () {
        var $active = $('#carousel div.wrap div.active');
        if ($active.length == 0) $active = $('#carousel div.wrap div:last');
        var $next = $active.next().length ? $active.next() : $('#carousel div.wrap div:first');
        $active.addClass('last-active');
        $next.css({ opacity: 0.0 })
				.addClass('active')
				.animate({ opacity: 1.0 }, 1000, function () {
				    $active.removeClass('active last-active');
				});
        $('#carousel .sequence li').removeClass('active');
        $('#carousel .sequence a').removeClass('active');
        var currentID = $next.attr('id');
        $('#carousel .sequence a[href$=' + currentID + ']').addClass('active').parent().addClass('active');
    }

    $('#carousel .sequence a').click(function () {
        clearInterval(sliderIntervalID);
        $('#carousel .sequence a').removeClass('active').parent().removeClass('active');
        $(this).addClass('active').parent().addClass('active');
        var clickedSlide = $(this).attr('href').replace("#", "");
        var $active = $('#carousel div.wrap div.active');
        $('#carousel div.wrap div.active').removeClass('active').addClass('last-active');
        $('#carousel div.wrap div').each(function () {
            if ($(this).attr('id') == clickedSlide) {
                $(this).css({ opacity: 0.0 }).addClass('active').animate({ opacity: 1.0 }, 1000, function () {
                    $active.removeClass('active last-active');
                });
            }
        });

        return false;
    });

    var sliderIntervalID = setInterval("slideSwitch()", 6000);

});

function initSlider(){
	$('.period').each(function(){
		var _a = -1;
		var _list = $(this).find('ul li');
		var text_box = $(this).find('.info-text');
		var active = $(this).find('.active');
		var firstUrl = $('.period ul li:first').children('a').attr('href');
		_list.each(function(_i){
			if($(this).hasClass('active')) _a = _i;
			$(this).hover(
			  function () {
					_list.eq(_a).removeClass('active').find('div').hide();
					$(this).addClass("active");
					if(text_box.length) text_box.html($(this).find('div').html());
						else $(this).find('div').show();
			  },
			  function () {
					$(this).removeClass('active').find('div').hide();
					_list.eq(_a).addClass('active');
					if(text_box.length) text_box.html(_list.eq(_a).find('div').html());
						else _list.eq(_a).find('div').show();

					if(_a == -1){
						$('.period li').removeClass('active').find('div').hide();
					}			
			  }
			);
		});
		if(_a != -1){
			if(text_box.length) text_box.html(_list.eq(_a).find('div').html());
			else _list.eq(_a).find('div').show();
		}
		$(this).find('.back').click(function(){
			var prevUrl = $(this).parent('.period').find('.active').prev().children('a').attr('href');
			if (!prevUrl) { prevUrl = firstUrl; }
			window.location.href = prevUrl;
			return false;
		});
		$(this).find('.next').click(function(){
			var nextUrl = $(this).parent('.period').find('.active').next().children('a').attr('href');
			if (!nextUrl) { nextUrl = firstUrl; }
			window.location.href = nextUrl;
			return false;
		});
	});
}
function ieHover(h_list) {
	if ($.browser.msie && $.browser.version < 7) {
		$(h_list).hover(function() {
			$(this).addClass('hover');
			if($(this).children('div').length) hideSelectBoxes($(this).children('div').eq(0));
		}, function() {
			$(this).removeClass('hover');
			if($(this).children('div').length) showSelectBoxes($(this).children('div').eq(0));
		});
	}
}
function hideSelectBoxes(object) {
	if ($.browser.msie && $.browser.version < 7) {
		var selects_list = $('select');
		object.each(function() {
			var _el = $(this);
			var t = _el.offset().top;
			var l = _el.offset().left;
			var w = _el.outerWidth();
			var h = _el.outerHeight();
			var el_selects = [];
			selects_list.filter(':visible').not(_el.find('select')).each(function(){
				var _select = $(this);
				var s_t = _select.offset().top;
				var s_l = _select.offset().left;
				var s_w = _select.outerWidth();
				var s_h = _select.outerHeight();
				var _ver = false, _hor = false;
				if((t - s_t > 0) ? (t - s_t < s_h) : (t - s_t + h > 0)) _ver = true;
				if((l - s_l > 0) ? (l - s_l < s_w) : (l - s_l + w > 0)) _hor = true;
				if(_ver && _hor) {
					_select.css('visibility', 'hidden');
					el_selects.push(this);
				}
			});
			this.sboxes = el_selects;
		});
	}
}
function showSelectBoxes(object) {
	if ($.browser.msie && $.browser.version < 7) {
		object.each(function() {
			if(this.sboxes.length > 0) {
				$(this.sboxes).css('visibility','visible');
			}
		});
	}	
}

function ToggleArticleDisplay(type)
{
    var full = document.getElementById("fullText");
    var paged = document.getElementById("pagedText");
    var fulllink = document.getElementById("FullLink");
    var pagedlink = document.getElementById("PagedLink");
    
    if (type == 1)
    {
        full.style.display = 'inline';
        paged.style.display = 'none';
        fulllink.style.display = 'none';
        pagedlink.style.display = 'block';
    }
    else
    {
        full.style.display = 'none';
        paged.style.display = 'inline';
        pagedlink.style.display = 'none';
        fulllink.style.display = 'block';        
    }     
}


 function submitForm()
 {
 	var submitForm = document.createElement("FORM");
	submitForm.Id = "searchbox_012637101302161472059:xhjhbftt5zc";
	document.body.appendChild(submitForm);
	submitForm.method = "GET";
	var newElement = document.getElementById("googleSearch");
	submitForm.appendChild(newElement);
	submitForm.action= "http://birth.com.au/GoogleSearchResults.aspx";
	submitForm.submit();
 }     
 
 function submitStreamsendRequest(metod, url)
 {
    document.forms[0].method = method;
    document.forms[0].action = url;
    document.forms[0].submit();
 }
 
 function fbs_click() 
 {
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
 }
 
 function openBrWindow(theURL,winName,features) {
	winName = window.open(theURL,winName,features);
	winName.focus()
}

function ShowHelp(div, title, desc)
{
	div = document.getElementById(div);
	div.style.display = 'inline';
	div.style.position = 'absolute';
	div.style.width = '176';
	div.style.color = '#000000';
	div.style.backgroundColor = '#FAFAFA';
	div.style.border = 'solid 1px #CCCCCC';
	div.innerHTML = '<div>' + desc + '</div>';
}

function HideHelp(div)
{
	div = document.getElementById(div);
	div.style.display = 'none';
}

function windowOpen(sURL, sTitle, sParams) {
 var s = sURL.replace(/-/,'%2D');
 var st = sTitle.replace(/-/,'_');
 var sti = st.replace('.', '_');
 window.open(s, sti, sParams)
}

