$(document).ready(function(){
	/*buggy browsers detecting*/
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" 
				&& parseInt(navigator.appVersion) == 4 
				&& navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" 
				&& parseInt(navigator.appVersion) == 4 
				&& navigator.appVersion.indexOf("MSIE 6.0") != -1 
				&& navigator.appVersion.indexOf("MSIE 8.0") == -1);
	var ie8 = (navigator.appName == "Microsoft Internet Explorer" 
				&& parseInt(navigator.appVersion) == 4 
				&& navigator.appVersion.indexOf("MSIE 8.0") != -1);
	
	
	if ($(window).width() < 960){
		$("body, .mainBack").css("width", "960px");
		$("body").css("background-position", "-610px 0")
	} else {
		$("body, .mainBack").css("width", "100%");
		$("body").css("background-position", "center 0")
	}
		
	/* background fix */
	$(window).resize(function(){
		if ($(window).width() < 960){
			$("body, .mainBack").css("width", "960px");
			$("body").css("background-position", "-610px 0")
		} else {
			$("body, .mainBack").css("width", "100%");
			$("body").css("background-position", "center 0")
		}
	})

	
	/* carousel init*/
	var carousel_prev_active = 0;
	var num_carousel_pages = 0;
	function carousel(carousel){
		if (jQuery.browser.msie){
			$(".itemText",carousel).css({"display":"block","opacity":"0.75"});
		};
		var time = (ie6)?1000:2500;
		num_carousel_pages = $(".item",carousel).length;
		var indexLength = $(".carouselNav .index",carousel).length;
		for (i = indexLength + 1; i <= num_carousel_pages; i++){
			$('<a class="index" style="margin-right:5px;" href="#">' + i + '</a>').insertBefore($(".carouselNav .next",carousel));
		};
		
		$(".carouselNav .index",carousel).live("click",function(e){
			e.preventDefault();
			carousel_prev_active = $(".carouselNav .index",carousel).index($(".active",carousel));
			$(".carouselNav .index",carousel).removeClass("active");
			$(this).addClass("active");
			var index_carousel = $(".carouselNav .index",carousel).index($(this));
			$(".innerHolder",carousel).css("background", "url(" + $(".item img",carousel).eq(carousel_prev_active).attr("src") + ")");
			$(".item, .itemText, .item img",carousel).stop().hide();
			if (jQuery.browser.msie){
				$(".itemText",carousel).css({"display":"none","opacity":"0"});
				$(".item img",carousel).eq(index_carousel).css({"display":"block","opacity":"1"});
				$(".item",carousel).eq(index_carousel).fadeIn(2500);
				$(".itemText",carousel).eq(index_carousel).css({"display":"block","opacity":"0"})
						.animate({"opacity":"0.75"}, 2500);
			
			} else {
				$(".item",carousel).eq(index_carousel).fadeIn(1);
				$(".itemText",carousel).eq(index_carousel).fadeIn(3000);
				$(".item img",carousel).eq(index_carousel).fadeIn(2500);
			}
			clearInterval(intervalCarousel);
			intervalCarousel = setInterval(carouselNext, 7000);
		});
		
		$(".carouselNav .prev, .carouselNav .next",carousel).click(function(e){
			e.preventDefault();
			var index_carousel = $(".carouselNav .index",carousel).index($(".carouselNav .active",carousel));
			carousel_prev_active = index_carousel;
			if (index_carousel != (num_carousel_pages-1) && index_carousel != 0) {
				index_carousel = ($(this).hasClass("prev"))?index_carousel-1:index_carousel+1;
			} else if (index_carousel == 0) {
				index_carousel = ($(this).hasClass("prev"))?num_carousel_pages-1:index_carousel+1;
			} else {
				index_carousel = ($(this).hasClass("prev"))?index_carousel-1:0;
			}
			$(".carouselNav .index",carousel).removeClass("active");
			$(".carouselNav .index",carousel).eq(index_carousel).addClass("active");
			$(".item, .itemText, .item img",carousel).stop().hide();
			$(".innerHolder",carousel).css("background", "url(" + $(".item img",carousel).eq(carousel_prev_active).attr("src") + ")");
			if (jQuery.browser.msie){
				$(".itemText",carousel).css({"display":"none","opacity":"0"});
				$(".item img",carousel).eq(index_carousel).css({"display":"block","opacity":"1"});
				$(".item",carousel).eq(index_carousel).fadeIn(2500);
				$(".itemText",carousel).eq(index_carousel).css({"display":"block","opacity":"0"})
						.animate({"opacity":"0.75"}, 2500);
			} else {
				$(".item",carousel).eq(index_carousel).fadeIn(1);
				$(".itemText",carousel).eq(index_carousel).fadeIn(3000);
				$(".item img",carousel).eq(index_carousel).fadeIn(2500);
			}
			clearInterval(intervalCarousel);
			intervalCarousel = setInterval(carouselNext, 7000);
		});
		
	}
	
	carousel($(".carouselHolder"));
	
	var intervalCarousel = setInterval(carouselNext, 7000);

	var prevRand = 0;
	function carouselNext(){
		var index_carousel_inner = $(".carouselNav .index").index($(".carouselNav .active"));
		if (index_carousel_inner != (num_carousel_pages-1)) {
			index_carousel_inner++;
		} else {
			index_carousel_inner = 0;
		}
		$(".carouselNav .index").eq(index_carousel_inner).click();
	} 
	
	/* ---------------- ie6 additional scripts ----------- */
	if (ie6 || ie55) {
		//fix images with png-source
		jQuery(this).find("img[src$=.png]").each(function() {

			jQuery(this).attr('width',jQuery(this).width());
			jQuery(this).attr('height',jQuery(this).height());

			var prevStyle = '';
			var strNewHTML = '';
			var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
			var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
			var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
			var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
			var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
			var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
			strNewHTML += imgStyle+'"></span>';
			if (prevStyle != ''){
				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;'+'">' + strNewHTML + '</span>';
			}
			jQuery(this).hide();
			jQuery(this).after(strNewHTML);

		});
		
		
		$(".contentColumn .submit").hover(function(){
			$(this).addClass("submithover");
		}, function(){
			$(this).removeClass("submithover");
		});
		
		if ($("#wrapper").height() < 740 ){
			$("#wrapper").css("height","740px");
		};
		
		// fix css background pngs
		jQuery(this).find("*").each(function(){
			var bgIMG = jQuery(this).css('background-image');
			if(bgIMG.indexOf(".png")!=-1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				jQuery(this).css('background-image', 'none');
				jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
			}
		});
	}
})
