﻿// hack for pamphlet dropdown list, cannot dynamically bind events on content pages for some reason.
function changeFolder() {
    var folderName = $("select#list-folders").val();
    //$(".viewport .content-scroll").html();
    $("#scroll").html();
    //var url = "http://das.murray.wa.gov.au/pamphlets/outputPamphlets2?folder=&section=";
    var url = "http://www.murray.wa.gov.au/pamphlets.aspx?section=";


    if (folderName != "View all") {
        folderName = folderName.replace(/ /gi, "%20");
        url += folderName;
    }

    $("#scroll").html("");
    $("#scroll").load(url + "&random=" + (new Date()).getTime());
    $('a.handle').css("left", "0px");
}
var i = 0;
$(document).ready(function () {

    // Establish page URL for other functions
    var pageAddress = window.location.pathname;

    if ($(".lozenges div").length <= 1) { $(".lozenges div").hide(); }


    /***************************************
    Page slideshow
    ***************************************/
    var slide_count = 0;
    $('.slideshow li').each(function () {
        if (!$('img', this).length) {
            $(this).remove();
        }
        else {
            slide_count++;
        }
    }).ready(function () {
        if (slide_count > 2) {
            $('.slideshow').addClass('activeSlideshow');
        }
        if (slide_count > 1) {
            $('.slideshow ul').cycle({
                fx: 'fade',
                speed: 'fast',
                timeout: 8000,
                pager: 'menu.controls',
                cleartypeNoBg: false
            });
        }
    });

    /***************************************
    Pamphlet rack
    ***************************************/
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    var sPathOnly = sPath.replace(".htm", "");
    $("#content").append("<div id='pamphlet-loader' class='hidden'></div>");



    $("select#list-folders").bind("change", function (e) {
        e.preventDefault();

        var folderName = $(this).val();
        //$(".viewport .content-scroll").html();
        $("#scroll").html();
        //var url = "http://das.murray.wa.gov.au/pamphlets/outputPamphlets2?folder=&section=";
        var url = "http://www.murray.wa.gov.au/pamphlets.aspx?section=";


        if (folderName != "View all") {
            folderName = folderName.replace(/ /gi, "%20");
            url += folderName;
        }

        $("#scroll").html("");
        $("#scroll").load(url + "&random=" + (new Date()).getTime());
        $('a.handle').css("left", "0px");

    });


    $('.pamphlets .ui-slider-handle').append('<div class="handle"></div>');

    $('#slideshow').css({ 'float': 'left', 'width': '384px' });

    $('<div id="details"></div>').appendTo('#content');

    $('#details').css({ 'float': 'right', 'width': '335px' });



    /***************************************
    Councillors
    ***************************************/
    var councillorLoops = 0;
    var cId = 1;
    $("#menuSUBJECT-LIST_level3").css({ 'width': '384px', 'float': 'left' });

    $("#menuSUBJECT-LIST_level3 li a").each(function () {
        var href = $(this).attr("href");
        var order = $(this).attr("onclick");

        var listItem = $(this).next().load(href + " #content div:not('.breadcrumb-trail')", function (data) {
            setThumbnail($(listItem), order);
            i++;
        });
    });
    function setThumbnail(myList, order) {

        order = order.split('(')[1];
        order = order.split(')')[0];

        $('<div id="details' + order + '" style="display:none"></div>').appendTo('#details');
        myList.parent().find("li").css({ 'float': 'left', 'width': '118px', 'listStyleType': 'none', 'margin': '0px 10px 10px 0px' });
        var htmlString = "";

        htmlString += myList.find(".photo").html();
        htmlString += myList.find(".thumbnail img").html();
        htmlString += myList.find(".description").html();
        htmlString += myList.find(".contact").html();
        $('#details' + order + '').html(htmlString);
        if (order == "1") {
            $('#details1').css({ 'display': 'block' });
        }
        cId++;
    }

    //$('<div style="background-color: #5B93C5; float: left; width: 384px; height: 24px; margin: 10px 10px 10px 0px;"><a href="" id="startslideshow">Play slideshow</a>&nbsp;<a href="" id="stopslideshow" style="display: none;">Stop slideshow</a></div>').insertAfter('#menuSUBJECT-LIST_level3');

    var t1 = '';
    var l1 = $('#menuSUBJECT-LIST_level3 li').length;
    var c1 = -1;
    var d1 = 2000;



    function StartSlideshow() {
        t1 = setInterval(function () {
            c1++;
            if (c1 < l1) {
                if (c1 > 0) {
                    $('#menuSUBJECT-LIST_level3 li:eq(' + (c1 - 1) + ')').find(".thumbnail").removeClass("active");
                }
                $('#menuSUBJECT-LIST_level3 li:eq(' + (c1) + ')').find(".thumbnail").addClass("active");
                $('#details').html($('#menuSUBJECT-LIST_level3 li:eq(' + c1 + ')').find(".photo").html() + $('#menuSUBJECT-LIST_level3 li:eq(' + c1 + ')').find(".description").html() + $('#menuSUBJECT-LIST_level3 li:eq(' + c1 + ')').find(".contact").html());
            } else {
                $('#menuSUBJECT-LIST_level3 li').find(".thumbnail").removeClass("active");
                c1 = -1;
            }
        }, d1);
    }

    function StopSlideshow() {
        clearTimeout(t1);
        c1 = -1;
    }

    $('#startslideshow').click(function (e) {
        $('#startslideshow').css('display', 'none');
        $('#stopslideshow').css('display', 'inline');
        StartSlideshow();
        e.preventDefault();
    });

    $('#stopslideshow').click(function (e) {
        $('#stopslideshow').css('display', 'none');
        $('#startslideshow').css('display', 'inline');
        $('#menuSUBJECT-LIST_level3 li').find(".thumbnail").removeClass("active");
        StopSlideshow();
        e.preventDefault();
    });

    /***************************************
    Dog pound
    ***************************************/
    var dogpoundLoops = 0;
    $("#dog-pound li a").each(function () {
        var href = $(this).attr("href");
        var order = $(this).attr("onclick");
        var next = $(this).next();
        var listItem = $(this).next().load(href + " #content div:not('.breadcrumb-trail')", function (data) {
            setTimeout(function () { setDogThumbnail($(listItem), order) }, 300);

            i++;
        });
    });

    function setDogThumbnail(myList, order) {

        order = order.split('(')[1];
        order = order.split(')')[0];

        $('<div id="details' + order + '" style="display:none" class="dogDetails"></div>').appendTo('#details');
        myList.parent().find("li").css({ 'float': 'left', 'width': '118px', 'listStyleType': 'none', 'margin': '0px 10px 10px 0px' });
        var htmlString = "";

        var ajaxElements = ".photo, .photo p, .thumbnail img, .description, .contact, .content_area";
        //myList.find(ajaxElements).addClass("hidden");
        myList.find(".box.alpha").remove();
        myList.find("a").parent().addClass("form-link");

        myList.find(".content_area").each(function (content) {
            htmlString += $(this).html();
        });

        $('#details' + order + '').html(htmlString);
        if (order == "1") {
            $('#details1').css({ 'display': 'block' });
        }
    }


    /**************************************
    Content pages
    ***************************************/
    $(".useful-links").each(function () {
        if ($(this).find("a").length > 0) { }
        else {
            $(this).remove();
        }
    });

    /***************************************
    FAQs
    ***************************************/
    if (pageAddress.indexOf("faqs.htm") > 0) {
        $(".content_area").addClass("faqs");
        $(".faqs p").slideUp();
        $("#id_search").quicksearch("h3");
        $(".faqs h3").append("<span class='expand-icon'></span>").bind("click", function () {
            $(".faqs h3.active").next("p").slideUp();
            $(".faqs h3.active").removeClass("active");
            $(".faqs .expand-icon").css("display", "block");
            $(this).addClass("active");
            $(this).next("p").slideDown();
            $(this).children(".expand-icon").css("display", "none");
        });
    }



    /*
    $('<div id="wrapper" style="float: left; width: 384px;">').insertBefore('#menuSUBJECT-LIST_level3');
    
    $('</div>').insertAfter('#menuSUBJECT-LIST_level3');
    
    $('<div id="slideshow"></div>').insertAfter('#menuSUBJECT-LIST_level3');

    $('#slideshow').css({'background-color':'#5B93C5', 'float':'left', 'width':'384px', 'height':'24px', 'margin':'10px 10px 10px 0px'});
    */

    /***************************************
    Page accordions
    ***************************************/

    $('.accordion').accordion({
        autoHeight: true
    });
    $('.accordion .ui-accordion-header:first').addClass('first');
    $('.accordion .ui-accordion-header:last').addClass('last');

    $('.tabs').tabs({
        selected: 0
    });


    $('.pamphlets .ui-slider-handle').wrap('<div class="slider-bar"></div>');
    $('.pamphlets .ui-slider-handle').append('<div class="handle"></div>');

    $('#main .wrap').eq(0).addClass('first');
    $('#footer .wrap').eq(0).addClass('first');
    $('.pamphlets li').eq(0).addClass('first');



    /***************************************
    form pages
    ***************************************/
    //if ($('#content a')){
    $('#content a').each(function () {
        var url = $(this).attr("href");
        if (url == undefined) {
            //stops IE8 js error
            return false;
        } else if (url.indexOf("facilities-booking.htm") > 0) {
            $(this).attr("href", url + "?facility=" + document.title.replace(/ /gi, "%20"));
        } else if (url.indexOf("public-consultation-form.htm") > 0) {
            $(this).attr("href", url + "?q=" + document.title.replace(/ /gi, "%20"));
        }
    });
    //};

    if (pageAddress.indexOf("facilities-booking.htm") > 0) {
        var url = $("iframe").attr("src");
        $("iframe").attr("src", url + "?facility=" + gup("facility"));
    }
    if (pageAddress.indexOf("public-consultation-form.htm") > 0) {
        $("#job-list").val(gup("q").replace(/%20/g, " ").replace(/%27/g, "'"));
    }

    $(".formLabel .formLabel:first").addClass("formTitle");

    $("input[title='Please enter a date in dd/mm/yyyy format']").datepicker();


});

/***************************************
Pamphlet rack scrollbar
***************************************/

function handleSliderSlide(e, ui) {
    var box_width = 0;
    $('.content-scroll li').each(function () {
        box_width = box_width + 107;
    });
    var maxScroll = box_width - $(".content-scroll").parent().width();
    $('.content-scroll').animate({
        marginLeft: '-' + (ui.value / 100) * 200
    }, 0);
    $('.pamphlets .ui-slider-handle').animate({
        marginLeft: '-' + (ui.value / 100) * 250
    }, 0);
}

function updateIFrame(height) {
	//alert(height);
	if(height != ''){
    var iframe = document.getElementById('the_iframe');
    iframe.setAttribute('height', height);}
}

function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];

} 
	slideshowPlaying = false;
	var currentSlide = 1;
	function loadProfile(id){
		slideshowPlaying = false;
		document.getElementById("slideshowBtn").innerHTML = "Play Slideshow"
		hideAllProfiles();
		currentSlide = id;
		document.getElementById("details" + id).style.display = "block";
		return false;
	}
	
	var slideshowDelay = 6000;
	
	function startStopSlideshow(){
		if(slideshowPlaying == false){
			setTimeout("scrollTabs()", slideshowDelay);
			slideshowPlaying = true;
			document.getElementById("slideshowBtn").innerHTML = "Stop Slideshow";
		}
		else{

			slideshowPlaying = false;
			document.getElementById("slideshowBtn").innerHTML = "Play Slideshow";
		}
	}
	
	function scrollTabs(){
		if(slideshowPlaying == true){
			var tempSlide = currentSlide + 1;
			hideAllProfiles();
			var tempProfile = document.getElementById("details" + tempSlide)
			if(tempProfile != null){
				tempProfile.style.display = "block";
				currentSlide = tempSlide;
				setTimeout("scrollTabs()", slideshowDelay);
			}
			else{
				currentSlide = 0;
				scrollTabs()
			}
		}
	}
	
	function hideAllProfiles(){
		for(var i = 0;i < 20; i++){
			var profile = document.getElementById("details" + i);
			if(	profile != null){
				profile.style.display = "none";
			}
		}
}



