$(document).ready(function(){
//$('body').append($("head link[rel='stylesheet']").attr('href'));
if ($('body:not(.admin)').length > 0) {
$('div.data').hide();
$('#loader').fadeIn('slow');
var test = window.location.pathname;
var error_div = "<table class='error' cellpadding='0' cellspacing='0'><tr><td><h1><img src='/public/images/page-not-found.png' alt='Page Not Found' /></h1><p>It appears this page does not exist.</p></td></tr></table>";
//alert(test);
var hash = window.location.hash.replace(/\#\!/, '');
//alert(hash);
if (hash.length == 0) {
    window.location = "/#!" + test;
} else {
    $.ajax({
        type: 'GET',
        url: hash + "?ajax",
        async: false,
        dataType: "xml",
            success: function(response){
                var head = document.getElementsByTagName('head')[0];
                //ADD CSS
                $("html head link[rel='stylesheet']:not([href='/public/css/style.css'])").remove();
                $('link', response).each(function(idx, lk) {
			var href = $(lk).attr('href').toLowerCase();
                        var cssNode = document.createElement('link');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.media = 'screen';
                        cssNode.href = href;
                        head.appendChild(cssNode)
		});
                //ADD SCRIPTS
                $('script', response).each(function(idx, lk) {
			var src = $(lk).attr('src').toLowerCase();
                        var scriptNode = document.createElement('script');
			scriptNode.type = 'text/javascript';
			scriptNode.src = src;
                        head.appendChild(scriptNode)
		});

                $("div.data").html($('data', response).text()).delay(250);
                document.title = $('title', response).text();
                
            },
            error: function(){
                //window.location.hash = "!/error";
                $("div.data").html(error_div);
                document.title = 'Kristina Taheri Special Events : Error';
            },
            complete: function(){
                $("#loader").fadeOut('fast', function(){
                    $("div.data").fadeIn('slow');
                });
                _gaq.push(['_trackPageview',hash]);
            }
    });
}

    $(window).hashchange(function(){
        $('div.data').fadeOut('slow', function(){
                
        var newhash = window.location.hash.replace(/\#\!/, '');
        if (newhash.length == 0) {
            newhash = "/";
        }
        //TWO MORE PARTS
        //1 - update stylesheets, scripts, etc.
        //2 - set Google Analytics to fire on an ajax request
        $.ajax({
            type: 'GET',
            url: newhash + "?ajax",
            async: false,
            dataType: "xml",
            success: function(response){
                
                var head = document.getElementsByTagName('head')[0];
                //ADD CSS
                $("html head link[rel='stylesheet']:not([href='/public/css/style.css'])").remove();
                $('link', response).each(function(idx, lk) {
			var href = $(lk).attr('href').toLowerCase();
                        var cssNode = document.createElement('link');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.media = 'screen';
                        cssNode.href = href;
                        head.appendChild(cssNode)
		});
                //ADD SCRIPTS
                $('script', response).each(function(idx, lk) {
			var src = $(lk).attr('src').toLowerCase();
                        var scriptNode = document.createElement('script');
			scriptNode.type = 'text/javascript';
			scriptNode.src = src;
                        head.appendChild(scriptNode)
		});

                $("div.data").html($('data', response).text()).delay(250);
                document.title = $('title', response).text();
                //$("div.data").html($('data', response).text());
            },
            error: function(){
                //window.location.hash = "!/error";
                $("div.data").html(error_div);
                document.title = 'Kristina Taheri Special Events : Error';
            },
            complete: function(){
                $('#loader').fadeIn('slow', function(){
                    $("div.data").fadeIn('slow');
                    $("#loader").fadeOut('fast');
                });
                _gaq.push(['_trackPageview',newhash]);
            }
        });
        });
        $('#loader').fadeIn('fast');
    });

/*@cc_on
(function () {
  var oldtitle = document.title || '';
  document.onpropertychange = function () {
    var newtitle
        , doctitle = document.title || '';

    if (window.event.propertyName != 'title' || doctitle == oldtitle) { return; }

    // clean the title if needed
    newtitle = doctitle.indexOf('#') != -1
      ? doctitle.substring(0, doctitle.indexOf('#'))
      : doctitle;

    if (newtitle == '' && doctitle.indexOf('#') != -1) {
      newtitle = oldtitle;
    }

    oldtitle = newtitle;
    document.title = newtitle;
  }
})();
@*/

$('body:not(.admin) a:not(.link)').live('click', function(){
    var destination = $(this).attr('href');
    window.location.hash = "!" + destination;
    return false;
});

$('#footer div.music').hide();
setTimeout( function() {
                    if ($.flash.available) {
                    $('#footer div.music').flash({
                                    swf: '/public/videos/audioplayer.swf',
                                    width: 100,
                                    height: 20,
                                    wmode: 'transparent',
                                    xmlfile: '/public/videos/playlist.xml'
                    });
                    $('#footer div.music').fadeIn('slow');
                }
                    },
                    2000);

//alert(hash);

//if (window.location.hash.length)
//{
////we will use $(window).location.hash.replace('#','') in the "data" section of the AJAX request
//
//alert(window.location.hash.replace('#',''));
//
//$.ajax({
// type: "get",
// url: window.location.hash.replace('#',''),
// dataType: "html",
// data:'',
// success: function(html) {
//                 //do something
// }  })
//}
//
//$('#book1').click(function(){
////add to hash data that you need to make the AJAX request later
//$(window).location.hash = $(this).attr('id');
//$.ajax({
// type: "POST",
// url: current_url,
// dataType: "html",
// data:'selection_id='+$(this).attr('id'),
// success: function(html) {
//                 //do something
// } })  })
}
});
