var $j = jQuery.noConflict();

$j(document).ready(function(){
	$j(".topnav-c li:last, .botnav-a li:last").addClass('last');
	
	/* COTTAGES */
	var CottagesBlockWidth = $j(".content-cottages-block").width();
	var CottagesListWidth = 0;
	$j(".content-cottages-list li").each(function(i){
		CottagesListWidth += $j(this).width();
	});
	if (CottagesListWidth <= CottagesBlockWidth) {
		$j(".content-cottages-block-scroll-right").hide();
		$j(".content-cottages-block-scroll-left").hide();
	}
	$j(".content-cottages-list").width(CottagesListWidth);
	
	$j(".content-cottages-block-scroll-right").click(function(){
		CottagesListMargin = parseInt($j(".content-cottages-list").css('margin-left'));
		if (CottagesListWidth + CottagesListMargin >  CottagesBlockWidth) {
			$j(".content-cottages-list").animate({
				marginLeft: (CottagesListMargin - 170) + "px"
			}, 200);
			return false;
		} else {
			$j(".content-cottages-list").animate({
				marginLeft: (CottagesBlockWidth - CottagesListWidth) + "px"
			}, 0);		
			return false;
		}
	});
	
	$j(".content-cottages-block-scroll-left").click(function(){
		CottagesListMargin = parseInt($j(".content-cottages-list").css('margin-left'));
		if (CottagesListMargin <  0) {
			$j(".content-cottages-list").animate({
				marginLeft: (CottagesListMargin + 170) + "px"
			}, 200);
			return false;
		} else {
			$j(".content-cottages-list").animate({
				marginLeft: "0px"
			}, 0);		
			return false;
		}
	});
	
	$j("a[class^='cottage-']").click(function(){
		cls = $j(this).attr('class').split('-');
		name = cls[1];
		$j("div[id^='cottage-']").hide();
		$j("#cottage-"+name).show();
		return false;
	});
	/* COTTAGES */
	
	
	/* COTTAGES 2*/
	var CottagesBlockWidth1 = $j(".content-cottages-block1").width();
	var CottagesListWidth1 = 0;
	$j(".content-cottages-list1 li").each(function(i){
		CottagesListWidth1 += $j(this).width();
	});
	if (CottagesListWidth1 <= CottagesBlockWidth1) {
		$j(".content-cottages-block-scroll-right1").hide();
		$j(".content-cottages-block-scroll-left1").hide();
	}
	$j(".content-cottages-list1").width(CottagesListWidth1);
	
	$j(".content-cottages-block-scroll-right1").click(function(){
		CottagesListMargin1 = parseInt($j(".content-cottages-list1").css('margin-left'));
		if (CottagesListWidth1 + CottagesListMargin1 >  CottagesBlockWidth1) {
			$j(".content-cottages-list1").animate({
				marginLeft: (CottagesListMargin1 - 170) + "px"
			}, 200);
			return false;
		} else {
			$j(".content-cottages-list1").animate({
				marginLeft: (CottagesBlockWidth1 - CottagesListWidth1) + "px"
			}, 0);		
			return false;
		}
	});
	
	$j(".content-cottages-block-scroll-left1").click(function(){
		CottagesListMargin1 = parseInt($j(".content-cottages-list1").css('margin-left'));
		if (CottagesListMargin1 <  0) {
			$j(".content-cottages-list1").animate({
				marginLeft: (CottagesListMargin1 + 170) + "px"
			}, 200);
			return false;
		} else {
			$j(".content-cottages-list1").animate({
				marginLeft: "0px"
			}, 0);		
			return false;
		}
	});
	
	$j("a[class^='cottage-']").click(function(){
		cls = $j(this).attr('class').split('-');
		name = cls[1];
		$j("div[id^='cottage-']").hide();
		$j("#cottage-"+name).show();
		return false;
	});
	/* COTTAGES */
	
	/* COTTAGES 3*/
	var CottagesBlockWidth2 = $j(".content-cottages-block2").width();
	var CottagesListWidth2 = 0;
	$j(".content-cottages-list2 li").each(function(i){
		CottagesListWidth2 += $j(this).width();
	});
	if (CottagesListWidth2 <= CottagesBlockWidth2) {
		$j(".content-cottages-block-scroll-right2").hide();
		$j(".content-cottages-block-scroll-left2").hide();
	}
	$j(".content-cottages-list2").width(CottagesListWidth2);
	
	$j(".content-cottages-block-scroll-right2").click(function(){
		CottagesListMargin2 = parseInt($j(".content-cottages-list2").css('margin-left'));
		if (CottagesListWidth2 + CottagesListMargin2 >  CottagesBlockWidth2) {
			$j(".content-cottages-list2").animate({
				marginLeft: (CottagesListMargin2 - 170) + "px"
			}, 200);
			return false;
		} else {
			$j(".content-cottages-list2").animate({
				marginLeft: (CottagesBlockWidth2 - CottagesListWidth2) + "px"
			}, 0);		
			return false;
		}
	});
	
	$j(".content-cottages-block-scroll-left2").click(function(){
		CottagesListMargin2 = parseInt($j(".content-cottages-list2").css('margin-left'));
		if (CottagesListMargin2 <  0) {
			$j(".content-cottages-list2").animate({
				marginLeft: (CottagesListMargin2 + 170) + "px"
			}, 200);
			return false;
		} else {
			$j(".content-cottages-list2").animate({
				marginLeft: "0px"
			}, 0);		
			return false;
		}
	});
	
	$j("a[class^='cottage-']").click(function(){
		cls = $j(this).attr('class').split('-');
		name = cls[1];
		$j("div[id^='cottage-']").hide();
		$j("#cottage-"+name).show();
		return false;
	});
	/* COTTAGES */
	/* COTTAGES 4*/
	var CottagesBlockWidth3 = $j(".content-cottages-block3").width();
	var CottagesListWidth3 = 0;
	$j(".content-cottages-list3 li").each(function(i){
		CottagesListWidth3 += $j(this).width();
	});
	if (CottagesListWidth3 <= CottagesBlockWidth3) {
		$j(".content-cottages-block-scroll-right3").hide();
		$j(".content-cottages-block-scroll-left3").hide();
	}
	$j(".content-cottages-list3").width(CottagesListWidth3);
	
	$j(".content-cottages-block-scroll-right3").click(function(){
		CottagesListMargin3 = parseInt($j(".content-cottages-list3").css('margin-left'));
		if (CottagesListWidth3 + CottagesListMargin3 >  CottagesBlockWidth3) {
			$j(".content-cottages-list3").animate({
				marginLeft: (CottagesListMargin3 - 170) + "px"
			}, 200);
			return false;
		} else {
			$j(".content-cottages-list3").animate({
				marginLeft: (CottagesBlockWidth3 - CottagesListWidth3) + "px"
			}, 0);		
			return false;
		}
	});
	
	$j(".content-cottages-block-scroll-left3").click(function(){
		CottagesListMargin3 = parseInt($j(".content-cottages-list3").css('margin-left'));
		if (CottagesListMargin3 <  0) {
			$j(".content-cottages-list3").animate({
				marginLeft: (CottagesListMargin3 + 170) + "px"
			}, 200);
			return false;
		} else {
			$j(".content-cottages-list3").animate({
				marginLeft: "0px"
			}, 0);		
			return false;
		}
	});
	
	$j("a[class^='cottage-']").click(function(){
		cls = $j(this).attr('class').split('-');
		name = cls[1];
		$j("div[id^='cottage-']").hide();
		$j("#cottage-"+name).show();
		return false;
	});
	/* COTTAGES */
	/* NEWS IMAGES */
	
	var GalleryBlockWidth = new Array;
	var GalleryListWidth = new Array;
	
	$j(".content-gallery-list").each(function(i){
		GalleryBlockWidth[i] = $j(this).parent().width();
		GalleryListWidth[i] = 0;
		$j(this).children("li").each(function(n){
			GalleryListWidth[i] += $j(this).width();
		});
		if (GalleryListWidth[i] <= GalleryBlockWidth[i]) {
			$j(this).parent().siblings().hide();
		}
		$j(this).width(GalleryListWidth[i]);
	});

	$j(".content-gallery-block-scroll-right").click(function(){
		GalleryListMargin = parseInt($j(this).siblings("div").children(".content-gallery-list").css('margin-left'));
		GalleryBlockWidth = $j(this).parent().width();
		GalleryListWidth = $j(this).siblings("div").children(".content-gallery-list").width();
		if (GalleryListWidth + GalleryListMargin >  GalleryBlockWidth) {
			$j(this).siblings("div").children(".content-gallery-list").animate({
				marginLeft: (GalleryListMargin - 170) + "px"
			}, 200);
			return false;
		} else {
			$j(this).siblings("div").children(".content-gallery-list").animate({
				marginLeft: (GalleryBlockWidth - GalleryListWidth) + "px"
			}, 0);		
			return false;
		}
	});
	
	$j(".content-gallery-block-scroll-left").click(function(){
		GalleryListMargin = parseInt($j(this).siblings("div").children(".content-gallery-list").css('margin-left'));
		GalleryBlockWidth = $j(this).parent().width();
		GalleryListWidth = $j(this).siblings("div").children(".content-gallery-list").width();
		if (GalleryListMargin <  0) {
			$j(this).siblings("div").children(".content-gallery-list").animate({
				marginLeft: (GalleryListMargin + 170) + "px"
			}, 200);
			return false;
		} else {
			$j(this).siblings("div").children(".content-gallery-list").animate({
				marginLeft: "0px"
			}, 0);		
			return false;
		}
	});	
	/* NEWS IMAGES */
	
	$j(".topnav-a li.passive").hover(function() {
		$j("#headline-banner-link").css("z-index", "auto");
	}, function() {
		$j("#headline-banner-link").css("z-index", "2");
	});


	function bling() {
		$j(".action_bnr .bg2").fadeOut(600, function() {
			$j(this).fadeIn(600);
		});
	}
	if ($j(".action_bnr").length > 0) {
		setInterval(bling, 1200);
	}

	/*var jSnow;
	function snow() {
		jSnow = $j().jSnow({
			flakes:30,
			flakeCode:["/templates/img/snow/snow1.png","/templates/img/snow/snow2.png","/templates/img/snow/snow3.png"]
		});
	}
	function stopSnow() {
		jSnow.kill();
	}

	snow();
	$j("#top").click(stopSnow);*/
});

function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for(i=0; i<embeds.length; i++)  {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if(embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if(html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i,"wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i,"<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin',new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if(!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase()=='window')
                new_embed.setAttribute('wmode','transparent');
            embed.parentNode.replaceChild(new_embed,embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for(i=0; i<objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if(object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if(html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i,"<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i,"<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for(j=0; j<children.length; j++) {
                if(children[j].getAttribute('name').match(/flashvars/i)) {
                    new_object = html.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i,"<param name='flashvars' value='"+children[j].getAttribute('value')+"' />");
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin',new_object);
            object.parentNode.removeChild(object);
        }
    }
}
