// when the DOM is ready...
$(document).ready(function () {
	//Clone navigation
	$("#menu a").each(function() { 
		$(this).attr({title:$(this).text()}); 
	}); 
	$("#menu").clone().appendTo("#bottom-navi").attr({id:"menu-ft"});

	//all external link in new window
	$('.content a[href^=http://], a#madeby').attr({target:"_blank"});
	
	//Let's add some spirales to panels
	$(".opening-image").not(".first").prepend('<div class="spirale spiraleLeft"></div>');
	$(".panel").not(".first").prepend('<div class="spirale spiraleLeft"></div>');
	
	// Lightbox
	var lightboxLvSettings = {BtnCloseText:'Aizv&#275;rt',txtImage:'Att&#275;ls',txtOf:'no'};
	var lightboxRuSettings = {BtnCloseText:'&#1047;&#1072;&#1082;&#1088;&#1099;&#1090;&#1100;',txtImage:'&#1048;&#1079;&#1086;&#1073;&#1088;&#1072;&#1078;&#1077;&#1085;&#1080;&#1077;',txtOf:'&#1080;&#1079;'};
	
	$('#lightbox-advantages a').lightBox(lightboxLvSettings);
	$('#lightbox-development a').lightBox(lightboxLvSettings);
	$('#lightbox a').lightBox(lightboxLvSettings);
	
	// Feedback Form
	$("#feedback-form").append('<a href="#" class="btnClose">close</a>');
	function centerForm(element){   
	    var imageHeight = $(element).height();   
	    var imageWidth = $(element).width();   
	    var windowWidth = $(window).width();   
	    var windowHeight = $(window).height();   
	    $(element).css({   
	        "position" : "absolute",   
	        "left" : windowWidth / 2 - imageWidth / 2,   
	        "top" : windowHeight /2 - imageHeight / 2   
	    });   
	};
	
	$('a#feedback').click(function() {
		centerForm("#feedback-form");
		$('#feedback-form').toggle('1000');
	});
	$('a.btnClose, a.close').click(function() {
		$('#feedback-form').fadeOut();
		return false;
	});
	$('#feedback-f').FormValidate();
	$("#feedback-f a.submit").click(function() { 
	    $('#feedback-f').submit(); 
	    return false; 
	});
	

	// Starting Coda Slider 
    var $panels = $('#slider .scrollContainer > div');
    var $container = $('#slider .scrollContainer');
	var $panelWidth = 711; //because $panels[0].offsetWidth doesn't work in Google Chrome

    // if false, we'll float all the panels left and fix the width 
    // of the container
    var horizontal = true;

    // float the panels left if we're going horizontal
    if (horizontal) {
        $panels.css({
            'float' : 'left',
            'position' : 'relative' // IE fix to ensure overflow is hidden
        });

        // calculate a new width for the container (so it holds all panels)
		$container.css('width', $panelWidth * $panels.length);
    }

    // collect the scroll object, at the same time apply the hidden overflow
    // to remove the default scrollbars that will appear
    var $scroll = $('#slider .scroll').css('overflow', 'hidden');
	
    // offset is used to move to *exactly* the right place, since I'm using
    // padding on my example, I need to subtract the amount of padding to
    // the offset.  Try removing this to get a good idea of the effect
	/*    var offset = parseInt((horizontal ? 
        $container.css('paddingTop') : 
        $container.css('paddingLeft')) 
        || 0) * -1; */
	var panelWidth = $("#slider").width();
	var offset = (($(window).width()/2) - 355)*(-1);
	
	
	var scrollOptionsFirst = {
        target: $scroll, // the element that has the overflow

        // can be a selector which will be relative to the target
        items: $panels,

        navigation: '.nav a',

        // allow the scroll effect to run both directions
        axis: 'xy',

        onAfter: trigger, // our final callback

        offset: offset,

        // duration of the sliding effect
        duration: 2000,

        // easing - can be used with the easing plugin: 
        // http://gsgd.co.uk/sandbox/jquery/easing/
        easing: 'swing'
    };
	
	$.localScroll(scrollOptionsFirst);
	
   /* // apply our left + right buttons
    $scroll
        .before('<img class="scrollButtons left" src="images/scroll_left.png" />')
        .after('<img class="scrollButtons right" src="images/scroll_right.png" />'); */

    // handle nav selection
    /* function selectNav() {
        $('.nav a.active').removeClass('active');
        $(this).addClass('active').append('.');
		
		//if ( $("li:has(this)") == $(".l2")) {alert("adf");
		//} else {
		//}
		var nextText = $(this).parent("li:last").find("li:first").children("a").html();
//		var nextText = $(this).parent().is(":last").find(":first").children("a").html();
		$('#next').html(nextText);
    } */
	
	function selectNav() {
		// Let's highligh (with class "active") our link
        $('.nav a.active').removeClass('active');
        $('a[@href=' + $(this).attr("href") +']').addClass('active');
		
		// define our Next and Previous links
		if ($("#menu a.active").parent().next().children("a").attr('href') != null){
			var nextLink = $("#menu a.active").parent().next().children("a");
		} else {
			var nextLink = $("#menu li:first a");
		}
		
		if ($("#menu a.active").parent().prev().children("a").attr('href') != null){
			var prevLink = $("#menu a.active").parent().prev().children("a");
		} else {
			var prevLink = $("#menu li:last a");
		}
		// and set parametrs to Next and Previous links
		
		$('a#prev').attr({href:prevLink.attr('href')});
		$('a#prev span').html(prevLink.text());
		
		$('a#next').attr({href:nextLink.attr('href')});
		$('a#next span').html(nextLink.text());
    }

/* $('a').click(function () {
		selectNav;
		return false;
	}); */

    // go find the navigation link that has this target and select the nav
    function trigger(data) {
        var el = $('.nav').find('a[href$="' + data.id + '"]').get(0);
        selectNav.call(el);
    }

    if (window.location.hash) {
        trigger({ id : window.location.hash.substr(1) });
    } else {
        $('.nav a:first').click();
    }

    var scrollOptions = {
        target: $scroll, // the element that has the overflow

        // can be a selector which will be relative to the target
        items: $panels,

        navigation: '.nav a',

        // selectors are NOT relative to document, i.e. make sure they're unique
        prev: $("a.prev"), 
        next: $("a.next"),

        // allow the scroll effect to run both directions
        axis: 'xy',

        onAfter: trigger, // our final callback

        offset: offset,

        // duration of the sliding effect
        duration: 500,

        // easing - can be used with the easing plugin: 
        // http://gsgd.co.uk/sandbox/jquery/easing/
        easing: 'swing'
    };

    // apply serialScroll to the slider - we chose this plugin because it 
    // supports// the indexed next and previous scroll along with hooking 
    // in to our navigation.
//    $('#slider').serialScroll(scrollOptions);

    // now apply localScroll to hook any other arbitrary links to trigger 
    // the effect
    $.localScroll(scrollOptions);

    // finally, if the URL has a hash, move the slider in to position, 
    // setting the duration to 1 because I don't want it to scroll in the
    // very first page load.  We don't always need this, but it ensures
    // the positioning is absolutely spot on when the pages loads.
    scrollOptions.duration = 1;
    $.localScroll.hash(scrollOptions);
	//on window resize
	$(window).bind("resize", function(){
		offset = (($(window).width()/2) - 355)*(-1);
		var scrollOptions = {
	        target: $scroll,
	        items: $panels,
	        navigation: '.nav a',
	        prev: $("a.prev"), 
	        next: $("a.next"),
	        axis: 'xy',
	        onAfter: trigger,
	        offset: offset,
	        duration: 500,
	        easing: 'swing'
	    };
		$.localScroll(scrollOptions);
		$('#menu a.active').click();
	
		centerForm("#feedback-form"); 
	});

});

