﻿window.addressInit = false;
window.curHash = '/';
window.pidsLoaded = false;

$(function () {
    if (window.pid == 1 && $('body').hasClass('blog') == false) {
        preloadPIDs();
        $.address.init(function (event) {
            if (window.addressInit == false) {
                $.address.state('/');
                window.addressInit = true;

                if (request('auto') == 'yes') {
                    $.address.value(request('slug'));
                } else if (window.pid > 1) {
                    location.href = '/?auto=yes&slug=' + window.slug;
                }
            }

        }).change(function (event) {
            value = $.address.state().replace(/^\/$/, '') + event.value;

            value = replace(value, '%23', '#');
            value = replace(value, 'http://cwl.champcms.ca', '');
            value = replace(value, 'http://www.churchwithoutlimits.com', '');
            value = replace(value, 'http://churchwithoutlimits.com', '');
            if (value == '') {
                value = '/';
            }
            fCurAddress = window.curHash;
            fCurAddress = replace(fCurAddress, '%23', '#');

            if (fCurAddress != value) {
                window.curHash = $.address.state() + '/#' + $.address.hash();
                loadPageByURL(value);
            } else {
            }
        });
        initHrefs();
    }
});

function initHrefs() {
    $('a[pid]').unbind('click');
    $('a[pid]').address();
}

function preloadPIDs() {
/*
    $.getJSON('/files/xml/pids.json', function (json) {
        window.pids = json;
        window.pidsLoaded = true;
    });
    */
}

function loadPage(fPID) {
    showLoader();
    $.getJSON(window.remoteURL + 'page-by-pid&linkid=' + fPID, function (json) {
        initPageJSON(json);
        hideLoader();
    });
}

function loadPageByURL(fURL) {
    if (window.pidsLoaded) {
        fOURL = fURL;
        fURL = replace(fURL, "-", "_");
        fURL = replace(fURL, "/", "_");
        json = window.pids['pid' + fURL];
        if (json == undefined) {
            loadPageViaRemote(fOURL);
        } else {
            initPageJSON(json);
        }
    } else {
        loadPageViaRemote(fURL);
       }
       _gaq.push(['_trackEvent', 'Page', 'load', fOURL]);

}

function loadPageViaRemote(fURL) {
    showLoader();
    $.getJSON(window.remoteURL + 'page-by-url&url=' + fURL, function (json) {
        initPageJSON(json);
        hideLoader();
    });
}

function initPageJSON(json) {
    if (json == undefined) {
    } else {
        if (json.status == 'OK') {
            showLoader();
            window.document.title = json.name + ' :: Church Without Limits :: Transforming Lives With the Love of God';
            $.preLoadImages(json.preload, function () {
            	switch (json.pid) {
            		case '1':
            			initCustomFunctions();
            			initHome();
            			transitionToHome(json);
            			$('#section-inside').html('');

            			break;
            		case '19':  /* Cell Locator */
            			setupInsideSlideshow(true, json.slides);
            			$('#section-inside').html(json.data);
            			map = null;
            			geocoder = null;
            			window.locations = json.locations;
            			setupInsideSlideshow(json.slides);
            			transitionToInside(function () {
            				initCellLocator();
            			});
            			break;
            		case '30':  /* Calendar */
            			setupInsideSlideshow(true, json.slides);
            			$('#section-inside').html(json.data);
            			setupInsideSlideshow(json.slides);
            			transitionToInside(function () {
            				initCalendar();
            			});
            			break;
            		case '155':
            			location.href = 'http://blog.churchwithoutlimits.com/';
            			break;
            		default:
            			if (json.scriptenabled == '1') {
            				$.get(json.script, function (data) {
            					fData = json.data;
            					fData = replace(fData, '{APP}', data);
            					json.scriptenabled = '0';
            					json.data = fData;
            					$('#section-inside').html(fData);
            				});
            			} else {
            				$('#section-inside').html(json.data);
            			}
            			setupInsideSlideshow(true, json.slides);
            			transitionToInside();

            			break;
            	}
            });
        }
    }
}

function setupInsideSlideshow(fDestroy, fSlides, fCallback) {
    if (fDestroy) {
        $('#slides').cycle('destroy');
        fHeight = 181;
    } else {
        fHeight = 443;
    }

    $('#slides').html(fSlides);
    animateSlideShowContainer(fHeight, function () {
        if (fDestroy == false) {
            initSlideShow();
        }

        if (fCallback == undefined) {
        } else {
            fCallback();
        }
    });

}

function animateSlideShowContainer(fHeight, fCallback) {
    $('#slides').animate({
        'height': fHeight
    }, 750, 'easeOutQuart', function () {
        fCallback();
    });
    
}


function transitionToHome(json) {
    if ($('body').hasClass('home') == false) {

        setBodyClass('home');
        $('.section.active').fadeOut(100, function () {
            $('.section.active').removeClass('active');
            $('#section-home').fadeIn(1200, function () {
                $('#section-home').addClass('active');
                setupInsideSlideshow(false, json.slides);
                hideLoader();
            });
        });
    } else {
        hideLoader();
    }
}

function transitionToInside(fCallBack) {
    doCufon();
    initCustomFunctions();
    initHrefs();
    setBodyClass('inside');
    $('.section.active').fadeOut(100, function () {
        $('.section.active').removeClass('active');
        $('#section-inside').fadeIn(1200, function () {
            $('#section-inside').addClass('active');
            initInside();

            if (fCallBack == undefined) {
            } else {
                fCallBack();
            }
            hideLoader();
        });
    });
}



function setBodyClass(fClass) {
    $('body').removeClass('splash home inside').addClass(fClass);
}
