$(document).ready(function(){
	$.featureList(
		$(".tabs li a"),
		$(".output li"), {
			start_item	:	0
		}
	);

	$('.block.text.large img').each(function(){
			var Url = $(this).parent().attr('href');
			if(Url != null){
				if(Url.search(/Image/) != -1){
					$(this).parent().addClass('fancy');
				}
			}
		});

	$("a.fancy").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200,
		'titlePosition' :	'over'
	});


	$(".list-toggle .item").each(function() {
		$(this).find(".category_name").click(function() {    	
			$(this).css("background-image", "url(/pics/open.png)");
			$(this).addClass("current");
			
			$(this).next("div:first").stop(true,true).slideToggle(function() {
				if($(this).css("display") == "none") {
					$(this).prev(".category_name").css("background", "url(/pics/closed.png) 0px 15px no-repeat");	
					$(this).prev(".category_name").removeClass("current");
				} else {
					$(this).prev(".category_name").css("background", "url(/pics/open.png) 0px 15px no-repeat");
					$(this).prev(".category_name").addClass("current");	
				}
			});
			
		});
	  });    
	
	$('.madeby a[href^="http://"]')	.attr({ target: "_blank" }); //Makes all links open in a new window
	$('.madeby a[href^="http://"].target')	.attr({ target: "_blank" }); //a link with the class of "target" opens in a new window
	
});

function FetchYouTubeFile(YouTubeUrl, DivForYouTube){
  if(YouTubeUrl != ''){
    var VideoID;
    var UrlCheckResult;

    UrlCheckResult = YouTubeUrl.match("[\\?&]v=([^&#]*)");

    VideoID = ( UrlCheckResult === null ) ? YouTubeUrl : UrlCheckResult[1];
    var CorrectUrl = "http://www.youtube.com/v/"+VideoID+""; 
    
    DivForYouTube.append('<object width="180" height="150"> \
                                  <param name="movie" value="' + CorrectUrl + '"></param> \
                                  <param name="wmode" value="transparent"></param> \
                                  <embed src="' + CorrectUrl + '" type="application/x-shockwave-flash" wmode="transparent" width="180" height="150"></embed> \
                                </object> \
                                ');
  }
}

function CheckImageSizes(){
  
  var CookieValue     = getCookie('ImageSizes');
  var aCookieValues   = null;
  var bUseCookieValue = false;
  
  if(CookieValue != 0){
    aCookieValues     = CookieValue.split('-');
    bUseCookieValue   = true;
  }
    
  var $BackgroundDiv = $('#background');
  var $BackgroundImage = $BackgroundDiv.find('img');
  var isiPad = navigator.userAgent.match(/iPad/i) != null;
  var BrowserHeight = parseInt($(window).height());
  var BrowserWidth = parseInt($(window).width());
  
  var BgImageHeight = parseInt($BackgroundImage.height());  
  var BgImageWidth = parseInt($BackgroundImage.width());
  
  if(!isiPad) {
    if(BrowserHeight > BgImageHeight) {  
      if(bUseCookieValue && bFirst){
        $BackgroundImage.css({ height : aCookieValues[1], width : aCookieValues[0] });
      } else {
        $BackgroundImage.css({ height : '100%', width : 'auto' });
      }
    }
    
    if(BgImageWidth < BrowserWidth) {
      if(bUseCookieValue && bFirst){
        $BackgroundImage.css({ height : aCookieValues[1], width : aCookieValues[0] });
      } else {
        $BackgroundImage.css({ height : 'auto', width : '100%' });
      }
    }  
    if(bFirst) {
      if(bUseCookieValue){
        bFirst = false;
        $("#background").css('visibility','visible');
      } else {
        $BackgroundDiv.css({ 'display':'none', 'visibility':'visible'});
        $BackgroundDiv.fadeIn('slow');
        bFirst = false;
      }
    }
  
  } else {
      
    if(bFirst){
      var SiteHeight = parseInt($("#site").height());
      var SiteWidth = parseInt($("#site").width());
      
      if(BgImageWidth < SiteWidth) {
        $BackgroundImage.css({ height : 'auto', width : '100%' }) 
      }
      
      if(SiteHeight > BgImageHeight) {
        $BackgroundImage.css({ height : '100%', width : 'auto' });
      }
      bFirst = false;
    } else {
            
      if(BgImageWidth < BrowserWidth) {
        $BackgroundImage.css({ height : 'auto', width : '100%' }) 
      }
      
      
      if(BrowserHeight > BgImageHeight) {
        $BackgroundImage.css({ height : BrowserHeight, width : 'auto' });
      }
    }
    
    $("#background").css('visibility','visible');
  }
  
  var ImageWidth  = $BackgroundImage.css('width');
  var ImageHeight = $BackgroundImage.css('height');

  setCookie('ImageSizes',ImageWidth + "-" + ImageHeight,365);
  
}
