$(document).ready( function() {
  $("a[rel=_blank]").attr("target","_blank");
  $("a[rel=_parent]").attr("target","_parent");
  $("a[rel=_self]").attr("target","_self");
  
	$("#content img[align=left]").css("margin","0px 10px 10px 0px");
	$("#content img[align=right]").css("margin","0px 0px 10px 10px");
	
	if($('.slideshow .images').length > 0 && $('.slideshow .images img').length > 0) {
		$('.slideshow .images').cycle({
			fx: 			'fade',
			speed:		2000,
			timeout:  5000
		});
	}
  
  $("a img").each( function() {
    $(this).parent().css("border", "0px");
  });
	
	if($('.slideshow .whiteback .itemsloop').length > 0) {
		$('.slideshow .whiteback .itemsloop').cycle({
			fx: 			'fade',
			speed:		1000,
			timeout:  4000
		});
	}
  
  $("#navigation ul ul").each( function() {
    $(this).css('display','block');
    $('a', this).css('width', '100%');
    var UlWidth = $(this).outerWidth();
    $('a', this).css('width', UlWidth);
    $(this).css({ display : 'none' });         
  });
  
  $("#navigation ul ul ul").each( function() {
    $(this).css('display','block');
    $(this).parent().parent().css('display','block');
    $('a', this).css('width', '100%');   
    var UlWidth = $(this).outerWidth(); 
    var NewLeft = parseInt($(this).parent().parent().outerWidth());
    if($.browser.msie && $.browser.version=="6.0") {
      $('a', this).each(function(){
        if($(this).outerWidth() > UlWidth){
          UlWidth = $(this).outerWidth();
        }
      })
    }
    $(this).css({ left : (NewLeft - 2) + "px" });         
    $('a', this).css('width', UlWidth);
    $(this).css({ display : 'none' });         
    $(this).parent().parent().css('display','none');
  });
  
  $('#navigation li').mouseenter(function(){
    $(this).find('ul').eq(0).show();
    $(this).find('a').eq(0).addClass('active');
  });
  
  $('#navigation li').mouseleave(function(){
    $(this).find('ul').hide();
    $(this).find('a').eq(0).removeClass('active');
  });
  
  if($.browser.msie && $.browser.version=="7.0") {
	  $("#site .site_border").css("margin-left", "-497px"); /* border site 1px difference ie7 hack */
  }

	if($.browser.msie && $.browser.version=="6.0") {		
		var NewHeight = $('#site').height();
		$('#site .site_back').height(NewHeight);
		$('#site .site_border').height(NewHeight);
		//$('#site .site_spacer').height(NewHeight);
	}
	
	$('#searchform input[type="text"], #newsletterform input[type="text"], #login input[type="text"], #login input[type="password"]').each(function() {
      $(this).focus( function() {
        var DefaultValue = $(this).attr("id");
        if($(this).val() == DefaultValue) {
          $(this).val("");
        }
      });
  
      $(this).blur( function() {
        if($(this).val() == "") {
          $(this).val($(this).attr("id"));
        }
      });
	});
		
	SetYoutubeVideos();
  SetYoutubeVideosList();
	
	$(".lightbox").fancybox({
		'padding'				: 0,
		'titleShow'			: false,
		'autoScale'			: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'width'					: 450,
		'height'				: 200,
		'type'					: 'iframe'
	});
		
	$("#footer .footer").click( function() {
		SlideAdvise();
	});
	
	$(window).resize( function() { 
		SlideAdviseResize(); 
		
		if($.browser.msie && $.browser.version=="6.0") {
			var NewHeight = $('#site').height();
			$('#site .site_back').height(NewHeight);
			$('#site .site_border').height(NewHeight);
			//$('#site .site_spacer').height(NewHeight);
		}
	});
});

function CheckNewsletter(Msg) {
	var bError = false;
  
  $('#newsletterform input[type="text"]').each( function() {
		if($(this).val() == $(this).attr("id")) {
			$('#newsletterform .errortxt').html(Msg);
      bError = true;
		}
	});
  
	if(bError) {
    return false;
  }
  return true;
}

function SlideAdvise() {
	var SiteHeight 				= $("#site").outerHeight();
	var HeaderHeight 			= $("#header").outerHeight();
	var NavigationHeight 	= $("#navigation ul").height();
	var FooterHeight 			= $("#footer .footer").height();
	
	var TotalHeight				= SiteHeight - HeaderHeight;
	$("#footer .footercontent").height(TotalHeight + "px");
	
	if($("#footer_container").height() != (TotalHeight + NavigationHeight)) {
		$("#footer_container .hidetext").css("display", "none");
		$("#footer_container .clicktxt").prepend("<div class=\"closebtn\"></div>");
		$("#footer_container").animate({
			"height" : TotalHeight + NavigationHeight + "px"
		}, "slow", function() {  });
    $('html, body').animate({ scrollTop: 0 }, "slow");
	} else {
		$("#footer_container").animate({
			"height" : FooterHeight + "px"
		}, "slow", function() {
			$("#footer_container .hidetext").css("display", "inline");
			$("#footer_container .closebtn").remove();
		});
	}
}

function SlideAdviseResize() {
	var SiteHeight 				= $("#site").outerHeight();
	var HeaderHeight 			= $("#header").outerHeight();
	var NavigationHeight 	= $("#navigation ul").height();
	var FooterHeight 			= $("#footer .footer").height();
	
	var TotalHeight				= SiteHeight - HeaderHeight;
	$("#footer .footercontent").height(TotalHeight + "px");
	if($("#footer_container").height() == FooterHeight) {
		$("#footer_container").animate({
			"height" : FooterHeight + "px"
		}, "slow", function() { });
    $('html, body').animate({ scrollTop: 0 }, "slow"); 
	} else {
		$("#footer_container").animate({
			"height" : TotalHeight + NavigationHeight + "px"
		}, "slow", function() {  });
	}
}

function SetYoutubeVideos() {
  
	if($(".youtubeblock").length > 0) {
    $(".youtubeblock .youtube").each( function() {
      var VideoId   = GetYoutubeVideoId($(this).attr("href"));
			$(this).html("<img style=\"width: 200px;\" src=\"http://img.youtube.com/vi/"+ VideoId +"/0.jpg\" alt=\"youtube\" />"); 
    });
  }
	
  $(".youtubeblock .overlay").click(function() { $('.youtube', $(this).parent()).trigger('click'); });                             
	$(".youtube").click(function() {
		var VideoId   = GetYoutubeVideoId($(this).attr("href"));
		$.fancybox({
				'padding'				: 0,
				'autoScale'			: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'					: this.title,
				'titleShow'			: false,
				'width'					: 854,
				'height'				: 480,
				'href'					: "http://www.youtube.com/v/" + VideoId + "&autoplay=1",
				'type'					: 'swf',
				'swf'						: {
				  'wmode'						: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});

		return false;
	});
	
  return ;
}

function SetYoutubeVideosList() {
  
  if($(".youtubeblocklist").length > 0) {
    $(".youtubeblocklist .youtubelist").each( function() {
      var VideoId   = GetYoutubeVideoId($(this).attr("href"));
      $(this).html("<img style=\"width: 100px;\" src=\"http://img.youtube.com/vi/"+ VideoId +"/0.jpg\" alt=\"youtube\" />"); 
    });
  }
  
  $(".youtubeblocklist .overlay").click(function() { $('.youtubelist', $(this).parent()).trigger('click'); });                             
  $(".youtubelist").click(function() {
    var VideoId   = GetYoutubeVideoId($(this).attr("href"));
    $.fancybox({
        'padding'        : 0,
        'autoScale'      : false,
        'transitionIn'  : 'none',
        'transitionOut'  : 'none',
        'title'          : this.title,
        'titleShow'      : false,
        'width'          : 854,
        'height'        : 480,
        'href'          : "http://www.youtube.com/v/" + VideoId + "&autoplay=1",
        'type'          : 'swf',
        'swf'            : {
          'wmode'            : 'transparent',
          'allowfullscreen'  : 'true'
        }
      });

    return false;
  });
  
  return ;
}

function GetYoutubeVideoId (URL) {
  if(URL === null){ 
    return ""; 
  }
  
  var VideoId;
  var Results;
  Results = URL.match("[\\?&]v=([^&#]*)");
  
  if(Results == null){
    Results = URL.split("/")  
    VideoId = ( Results === null ) ? URL : Results[3];  
  } else {
    VideoId = Results[1]
  }

  return VideoId;
}
