﻿$(document).ready(function(){
	$.ajax({
		type: "GET",
		url: "setup/setup.xml",
		dataType: "xml",
		success: function(xml) {				
			$(xml).find('setup').each(function(){
				var banners = $(this).find('banners');
				$(banners).find('banner').each(function(){
					var display = $(this).attr('display');
					var id = $(this).attr('id');
					if(display != "true"){
						$('#'+id).remove();
					} else{
						$('#'+id).css("display","block");
					}
				});
				
				$(xml).find('styling').each(function(){
					var bgcolor = $(this).find('background-color').attr('value');
					var footercolor = $(this).find('footer-color').attr('value');
					var bgimg = $(this).find('background-image').attr('value');
					if(bgcolor && !bgimg){
						$('#wrapper').css("background-color", bgcolor);
						if(footercolor){
							$('#footer').css("color", footercolor);
							$('#footer a').css("color", footercolor);
						};
					};
					if(bgimg){
						$.backstretch(bgimg);
						if(footercolor){
							$('#footer').css("color", footercolor);
							$('#footer a').css("color", footercolor);
						};
					};
				});
				// Check to see if there are crosspromos, if any
				var crosspromos = $(this).find('crosspromos');
				var crosspromotxt = $(this).find('crosspromos').attr('text');
				var crosspromodisplay = $(this).find('crosspromos').attr('display');
				if(crosspromos.children().length > 0 && crosspromodisplay == "true"){
					//alert("No. of promos: "+crosspromos.children().length);
					// Process individual promos, create their DIVs
					$(crosspromos).find('promo').each(function(){
						var id = $(this).attr('id');
						var display = $(this).attr('display');
						var image = $(this).attr('image');
						var title = $(this).attr('title');
						var text = $(this).attr('text');
						var link = $(this).attr('link');
						if(display == "true"){
							if(!$('#crosspromo_area').length){
								// Create crosspromos holder DIV, into which individual promos will be inserted
								$('#content').prepend('<div id="crosspromo_area"><div id="promoheader"><h3>'+decode(crosspromotxt)+'</h3></div></div>');
							};
							//$('#flashcontent').after('<div class="promo" id="promo_'+id+'"></div>');
							// Check if crosspromos holder DIV exists, if so, start inserting promos into it
							if($('#crosspromo_area').length){							
								$('<div class="promo" id="promo_'+id+'"><div class="promoimage"><div class="wrap"><a href="'+link+'"><img src="'+image+'" alt="'+title+'"></a><div class="front">'+text+'</div></div></div><div class="promotitle"><a href="'+link+'">'+title+'</a></div></div>').appendTo('#crosspromo_area');
							};
						};
					});
					$(function() {
					    $('.wrap').hover(function() {
					        $(this).find('.front').stop().animate({ "top" : '100px'}, 200);   
					    }, function() {
					        $(this).find('.front').stop().animate({ "top" : '150'}, 300);       
					    });
					});
				};
			});
		}
	});
});

function decode(html) {
    var encoded = {
        "lt" : "<",
        "gt" : ">",
        "quot" : "\"",
        "#37" : "'",
        "apos" : "'"
    };
    for (var i in encoded) {
        html = html.replace(new RegExp("&" + i + ";", "gi"), encoded[i]);
    }
    return html;
}

(function($) {
  $.backstretch = function(src, options, callback) {
    var settings = {
          hideUntilReady: true, // Hide the image until it's finished loading
          speed: 0 // fadeIn speed for background after image loads (e.g. "fast" or 500)
        },
        imgRatio;
    
    // Extend the settings with those the user has provided
    if(options && typeof options == "object") $.extend(settings, options);
    
    // Initialize
    $(document).ready(_init);
  
    // For chaining
    return this;
    
    function _init() {
      // Prepend image, wrapped in a DIV, with some positioning and zIndex voodoo
      if(src) {
        var commonCSS = {left: 0, top: 0},
            wrap = $("<div />").attr("id", "backstretch-wrap")
                               .css( $.extend(commonCSS, {position: "absolute", zIndex: -1}) ),
            container = $("<div />").attr("id", "backstretch")
                                    .css( $.extend(commonCSS, {position: "fixed", overflow: "hidden", zIndex: -1}) )
                                    .appendTo(wrap),
            img = $("<img />").attr("src", src)
                              .bind("load", function() {
                                    var self = $(this);
                                    imgRatio = self.width() / self.height();
                                    _adjustBG(function() {
                                      if( settings.hideUntilReady )
                                        self.fadeIn(settings.speed, function(){
                                          // Callback, if necessary
                                          if(typeof callback == "function") callback();
                                        });
                                    });
                                  });
        
        if(settings.hideUntilReady) img.hide();          
        img.appendTo(container);
          
        $("body").prepend(wrap);

        // Adjust the background size when the window is resized
        $(window).resize(_adjustBG);
      }
      
    }
    
    function _adjustBG(callback) {
      var bgWidth = $(window).width(),
          bgHeight = bgWidth / imgRatio;

      if(bgHeight < $(window).height()) {
        bgHeight = $(window).height();
        bgWidth = bgHeight * imgRatio;
      }

      $("#backstretch img").width( bgWidth ).height( bgHeight );
      
      if (typeof callback == "function") callback();
    }
  };
  
})(jQuery);
