$(document).ready(function() {

	/* fancybox settings */
	$("a.group").fancybox({
		'speedIn'		:	300, 
		'speedOut'		:	300, 
		'overlayShow'	:	true,
		'titlePosition'	:	'outside',
		'autoScale'		:	'false'
	});

	$("a.iframe").fancybox({
		'type'			: 	'iframe',
		'height'		:	'80%',
		'width'			:	'80%'
	});
	
    // dropdown menu
    $("ul>li.hasChildren #textItem").wrap('<span>');
    
    $("ul>li.hasChildren span").click(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.subnav").first().slideDown('fast').show(); //Drop down the subnav on click

        $(this).parent().hover(function() {
        }, function(){
                elem = this;
                $(elem).parent().find("ul.subnav").slideUp('slow');
        });

    });

	// init nivo slider
    $('#slider').nivoSlider({
        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
        slices:1,
        animSpeed:500, //Slide transition speed
        pauseTime:5000,
        startSlide:0, //Set starting Slide (0 index)
        directionNav:true, //Next & Prev
        directionNavHide:false, //Only show on hover
        controlNav:true, //1,2,3...
        controlNavThumbs:false, //Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, //Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', //Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
        keyboardNav:true, //Use left & right arrows
        pauseOnHover:true, //Stop animation while hovering
        manualAdvance:false, //Force manual transitions
        captionOpacity:1, //Universal caption opacity
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){}, //Triggers after all slides have been shown
        lastSlide: function(){}, //Triggers when last slide is shown
        afterLoad: function(){} //Triggers when slider has loaded
    });

    /*	carousel */
    if($('#carouselHolder').css('display') != "none") {
        jQuery('#bigCarousel').jcarousel({
        	wrap: 'both',
        	scroll: 1,
        	visible: 1,
        	auto: 5,
        	initCallback: bigCarousel_initCallback,
        	reloadCallback: function() { $('#carouselHolder').fadeIn() },
            buttonNextHTML: null,
            buttonPrevHTML: null,
            itemFallbackDimension: $(document).width(),
            itemVisibleInCallback: updateNavi
        });
        
        carouselRef = jQuery('#bigCarousel').data('jcarousel');
    }
    
    origWidth = $(window).width();
    $('.jcarousel-item').css({width: origWidth});
    $('#carouselHolder').css({ width: $(window).width() });
    
    // news ticker
    $("div#newsTicker ul").liScroll({travelocity:0.08}); 

    // frontpage rollovers
    $("img.colorMe")
    .mouseover(function() { 
        src = $(this).attr("src").match(/[^\.]+/) + "_color.jpg";
        $(this).attr("src", src);
    })
    .mouseout(function() {
        var src = $(this).attr("src").replace("_color", "");
        $(this).attr("src", src);
    });

    $(window).bind('resize', function() {

		$('#carouselHolder').hide();
		$('.jcarousel-item').css({width: $(window).width()});
		$('#carouselHolder').css({width: $(window).width()});

		reloadTimeout = setTimeout("reloadCarousel()", 500);
    	
    });

});

function reloadCarousel() {
	
	clearTimeout("reloadTimeout");
	carouselRef.reload();
	
}

function bigCarousel_initCallback(carousel) {

    jQuery('#carouselHolder-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#carouselHolder-prev').bind('click', function() {
        carousel.prev();
        return false;
    });

    // setup navi
    if(carousel.options.size) {
	    for(i = 1; i < (carousel.options.size + 1); i++) {
	    	var htmlStr = $('#carouselNavi').html() + '<span class="slideSelector'+ (i == 1 ? " on" : "") +'" id="' + i +'"></span>';
	    	$('#carouselNavi').html(htmlStr);
	    }
	
	    // position navi
	    var marginLeft = parseInt($('#carouselNavi').css('marginLeft')) - parseInt($('#carouselNavi').css('width')) - 10;
	    $('#carouselNavi').css({'marginLeft': marginLeft});
	    setTimeout("$('#carouselNavi').fadeIn({duration: 1000})", 300); 
    
	    // setup selector actions
	    $('#carouselNavi span.slideSelector').click(function() {
	    	carousel.scroll(jQuery.jcarousel.intval(this.id));
	    	return;
	    });
    }
    
	carousel.clip.hover(
		function() {
			carousel.stopAuto();
		},
		function() {
			carousel.startAuto();
		}
	);
    
};

function updateNavi(carousel, item, index) {
	
	$('#carouselNavi span').removeClass('on');
	$('#carouselNavi span#' + carousel.first).addClass('on');
	
}

function checkMiniContact() {
	
	var required = Array('Name', 'Email', 'Message', 'Spamcheck');
	
	errors = Array();
	$.each(required, function(i, elem) {
		if($('#' + elem).val() == "" && elem != "Spamcheck") {
			errors.push(elem);
		}
		if(elem == "Spamcheck" && ($('#' + elem).val() != "five" && $('#' + elem).val() != "viisi")) {
			errors.push("Spam check");
		}
		
	});

	if(errors.length > 0) {
		alert("Please fill in the following fields:\n\n" + errors.join("\n"));
		return false;
	}
	else {
		return true;
	}

	return false;
	
}


function checkform() {
	
	var required = new Array(
		"nimi",
		"email",
		"company",
		"viesti",
		"spamcheck"
	);
	var errors = 0;
	
	$.each(required, function() {
		
		if($('#' + this).val() == "") {
			$('#' + this).addClass("error");
			errors++;
		}
		else if(this == "spamcheck" && $('#' + this).val() != "five") {
			$('#' + this).addClass("error");
			errors++;
		}
		else {
			$('#' + this).removeClass("error");
			errors = errors > 0 ? errors-- : 0;
		}

	});
	
	if(errors > 0) {
		$("#fillall").show();
	}
	else {
		$("#fillall").hide();
		$("#contactform").submit();
	}
}

function googleAnalytics() {
	document.write('<div style="position: absolute; background: red; color: white; padding: 5px;">GA</div>');
}

function adKiiviSignatureCheck() {
	document.write('<div style="position: absolute; background: magenta; color: white; padding: 5px; top: 30px;">SIGI</div>');
}
