﻿// Prefix
var ajaxPrefix = String(window.location).replace(/randomhouse.com.au\/[\S]+/, "randomhouse.com.au/");

// Embed video
function EmbedVideo() {
	var Split = String($(this).attr("href")).split("v=");
	var VideoID = Split[1];
	var EmbedCode = '<iframe width="650" height="400" src="http://www.youtube.com/embed/' + VideoID + '?autoplay=1" frameborder="0" allowfullscreen></iframe>';
	$("#VideoPlayerInner").html(EmbedCode);
}

// Show video
function ShowVideo() {
	$("#VideoPlayerInner").show();
}

// Hide video
function HideVideo() {
	$("#VideoPlayerInner").hide();
}

// Document ready
$(document).ready(function () {
	
	$(".DropBox").selectBox();
	$(".DropBox").selectBox('settings', {
		'menuTransition': 'fade',
		'menuSpeed': 'fast'
	});
	
	// Tabbing loop
	/*$('ul.tabNavigation').each(function () {
		var tabContent = $("." + $(this).attr("rel"));
		tabContent.hide();
		tabContent.first().show();

		// Doing this with inline css now
		// Check Top10.ascx

	});*/

	// Tabbing click
	$('ul.tabNavigation a').click(function () {
		var tabContent = $("." + $(this).parents("ul.tabNavigation").attr("rel"));
		tabContent.hide();
		tabContent.filter(this.hash).show();
		$(this).parents(".tabNavigation").find("a").removeClass('Selected');
		$(this).addClass('Selected');
		tabContent.filter(this.hash).find('img').each(function () {
			$(this).attr('src', $(this).parent().attr('rel'));
		});
		return false;
	});

	// YouTube Videos
	$("body").append('<div style="display:none;"><div id="VideoPlayer" style="width:650px; height:400px;"><div id="VideoPlayerInner" style="display:none;"></div></div></div>');
	$("a.YouTube").click(EmbedVideo);
	$("a.YouTube").each(function () {
		$(this).fancybox({
			'scrolling': 'no',
			'titleShow': false,
			'titlePosition': 'inside',
			'transitionIn': 'elastic',
			'transitionOut': 'elastic',
			'easingIn': 'easeOutBack',
			'easingOut': 'easeInBack',
			'speedIn': 500,
			'speedOut': 500,
			'onComplete': ShowVideo,
			'onClosed': HideVideo,
			'href': "#VideoPlayer"
		});
	});

	//    stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
	//    if (navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
	//        $("#CommunityStrip").css({ "position": "relative" });
	//        //$("#CommunityStrip").css("width", $(window).width());
	//        $(document).scroll(OnScroll);
	//        $(document).scrollstart(ScrollStart);
	//        OnScroll();
	//    };

	//    function ScrollStart() {
	//        $("#CommunityStrip").stop().css({ opacity: 0 });
	//    }

	//    function OnScroll() {

	//        $("#CommunityStrip").css("top", $(document).scrollTop());
	//        $("#CommunityStrip").stop().css({ opacity: 1 });
	//    }

	// Gallery
	$("a.FancyGallery").fancybox();
	$("a.Modal").fancybox({
		'hideOnContentClick': false,
		//type: 'iframe',
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'speedIn': 600,
		'speedOut': 200
	});

	// Custom Style Dropdowns

	// Run the predictive search function, please check
	// includes/js/randomhouse.predictivesearch.js
	// for code and documentation
	$('#Finder').predictiveSearch({ saveBandwidth:true });
    
    // Clears Input Fields
    $('.default-value').each(function() {
        var default_value = this.value;
        $(this).focus(function() {
            if(this.value == default_value) {
                this.value = '';
            }
        });
        $(this).blur(function() {
            if(this.value == '') {
                this.value = default_value;
            }
        });
    });



});

// Run highlights plugin on window.load
$(document).ready(function () {
	(function () {
		// Run feature slider
		if ($(".ModularSlider.FeatureSlider").length > 0) $(".ModularSlider.FeatureSlider").highlights();

		// Run banner slider
		if ($(".ModularSlider.BannerSlider").length > 0) $(".ModularSlider.BannerSlider").highlights();

		// Run footer slider
		if ($(".ModularSlider.FooterSlider").length > 0) $(".ModularSlider.FooterSlider").highlights({ AddCircles: false });
		
		
	})();
});

// Advanced Search (Toggle)
$(document).ready(function () {
    $("#Keyword").select();
    $('.AdvancedSearchLink').click(function () {
        $(this).parent().find('.AdvancedSearchFrom').toggle();
        $(this).toggleClass('ArrowUp');
        $(this).toggleClass('ArrowDown');
        $("#Keyword").select();
    });
    $('input[value="Reset"]').click(function () {
//        $("#AdvancedSearch select").each(function () {
//            $(this).val($(this).children("option:first").val());
//        });
        $("#AdvancedSearch input:text").attr("value", "")
    });
});

// Assist in clearing RightColumn pods when responsive css is active
$(document).ready(function () {
	$('#RightColumn > div').addClass("FirstChild");
	$('#RightColumn .FirstChild:even').addClass("EvenChild").before('<div class="RightColumnDivider Sprite BorderRight"></div>');
});

// Correct flash advertisements displaying above all other content regardless of z-index
$(document).ready(function () {
	FlashHeed.heed();
});
