﻿jQuery(document).ready(function() {
    
    call_lightbox_clone("auto");
    kriesi_start_slideshow();


});


function kriesi_start_slideshow() //preloading images for the slideshow, then calling the slideshow
{
    var $images = jQuery('.feature_image_container img');
    var $everything_loaded = true;


    $images.each(function(i) {
        if ($images[i].complete == false || $images[i].complete == undefined || $images[i].complete == null) {
            $everything_loaded = false;
        }
    });


    if ($everything_loaded) {
        kriesi_amplify_slideshow();
    }
    else {
        window.setTimeout('kriesi_start_slideshow()', 1000);
    }
}




function kriesi_amplify_slideshow() {

    if (jQuery("#feature_area").length == 0) {
        active_rotation = setInterval(function() { }, 100000000); //fake_var
        return false
    } //check if this is starting page

    jQuery(".feature_thumbs").fadeIn(1400);


    var thumbs = jQuery(".feature_thumbs .amplify_thumb");
    var articles = jQuery(".text_container");
    var autorotate = jQuery("meta[name=amplify1]").attr('content');
    var autorotate_duration = jQuery("meta[name=amplify2]").attr('content');

    if (thumbs.length > 1 && autorotate == 2) //check if rotation should be started?
    {
        active_rotation = setInterval(amplify_autorotate, autorotate_duration);
        //change the number for a different pause between slides, 
        //has to be at least 2000 if you have more than 6 slides

    }
    else {
        if (thumbs.length == 1) {
            jQuery(".feature_thumbs").css({ display: "none" }); // if only one thumb hide it
        }
        active_rotation = setInterval(function() { }, 100000000); //fake_var
    }


    var transition_duration = 500;
    var transition_easing = "easeOutBack";
    var autorotate = 0;
    var current_thumb_pos = 0;
    var thumbsdisplayed = 5;
    var modifier = 1;
    var may_change = 1;

    jQuery(".feature_thumbs .amplify_thumb").bind("click", amplify_rotation);

    if (thumbsdisplayed < thumbs.length) {
        jQuery(".feature_image").prepend('<a href="" class="prev_thumbs">Prev</a><a href="" class="next_thumbs">Next</a>');

        jQuery(".next_thumbs").css({ display: "none" }).fadeIn(2200).bind("click", "next", amplify_change_thumbs);
        jQuery(".prev_thumbs").css({ display: "none" }).fadeIn(2200).bind("click", "prev", amplify_change_thumbs);
    }


    function amplify_autorotate() {
        autorotate = jQuery(".feature_thumbs .amplify_thumb").length - 1 <= autorotate ? 0 : autorotate + 1;
        jQuery(".feature_thumbs .amplify_thumb:eq(" + autorotate + ")").trigger("click", ["auto"]);

        if (autorotate + 2 > thumbsdisplayed * modifier || autorotate + 2 > jQuery(".feature_thumbs .amplify_thumb").length) {
            jQuery(".next_thumbs").trigger("click", ["auto"]);
        }

    }

    function amplify_change_thumbs(click_event, auto) {
        if (auto != "auto") {
            clearInterval(active_rotation);
        }
        thumbnails = jQuery(".feature_thumbs .amplify_thumb_wrap");
        show_segment = thumbsdisplayed * modifier;
        current_thumb_pos = show_segment - thumbsdisplayed;
        modifier = click_event.data == "next" ? modifier + 1 : modifier - 1;

        if (modifier == 0) {
            modifier = thumbnails.length / thumbsdisplayed;
            modifier = Math.ceil(modifier);
        }


        if (modifier > Math.ceil(thumbnails.length / thumbsdisplayed)) {
            modifier = 1;
        }



        thumbnails.slice(show_segment, thumbnails.length).css({ display: "none" });
        thumbnails.slice(current_thumb_pos, show_segment).each(function(i) {
            jQuery(this).fadeOut(transition_duration, function() {
                show_segment = thumbsdisplayed * modifier;
                current_thumb_pos = show_segment - thumbsdisplayed;

                thumbnails.slice(current_thumb_pos, show_segment).fadeIn(transition_duration);
            });

        });

        current_thumb_pos += thumbsdisplayed;
        return false;
    }


    function amplify_rotation(event, $auto) {

        if (may_change == 0) return false;

        may_change = 0;
        if ($auto != "auto") {
            clearInterval(active_rotation);
        }

        $image = jQuery(this);
        if ($image.hasClass("current_image")) { may_change = 1; return false };
        jQuery(".current_image").removeClass("current_image");
        $image.addClass("current_image");
        /*positioning of the soon to be cloned image*/
        $image_pos = $image.offset();
        $container = jQuery("#feature_area").offset();
        $new_pos_top = $image_pos.top - $container.top;
        $new_pos_left = $image_pos.left - $container.left;

        $imgwrap_y = jQuery(".imgwrap").height();
        $imgwrap_x = jQuery(".imgwrap").width();

        $animate_to = jQuery(".feature_image_container").offset();

        $own_center_y = $image.height() / 2;
        $own_center_x = $image.width() / 2;

        $animate_to_center_y = jQuery(".feature_image_container").height() / 2;
        $animate_to_center_x = jQuery(".feature_image_container").width() / 2;

        $step1 = $animate_to.top - $container.top + $animate_to_center_y - $own_center_y;
        $step2 = $animate_to.left - $container.left + $animate_to_center_x - $own_center_x;

        $step3 = $animate_to.top - $container.top + 1; //+1 is a modifier because the new image does not have a border
        $step4 = $animate_to.left - $container.left + 1;

        $number_of_image = $image.attr('id').replace(/amplify_thumb_/, "");


        /*cloning image and apllying styles*/
        $slidingImage = $image.clone().appendTo(jQuery("#feature_area")).css(
		{
		    position: "absolute",
		    top: $new_pos_top,
		    left: $new_pos_left,
		    zIndex: 12
		});

        //not used anymore, could be used with resizing script
        //$img = $slidingImage.find(".thumbwrap img");
        //$img.attr('src',$img.attr('alt'));


        /*changing text that is displayed*/
        jQuery('.text_container:visible').fadeOut(transition_duration, function() {
            jQuery(this).addClass('hidden').css({ display: "none" });
            jQuery("#text_container_" + $number_of_image).css({ display: "none" }).removeClass('hidden').fadeIn(transition_duration / 2);
        });

        $slidingImage.animate(
		{
		    top: $step1, left: $step2

		}, transition_duration, transition_easing,
		function() {



		    jQuery(this).css({ zIndex: 3 }).animate(
			{
			    top: $step3,
			    left: $step4,
			    width: $animate_to_center_x * 2,
			    height: $animate_to_center_y * 2
			},
				transition_duration, transition_easing);

		    call_lightbox_clone($auto);



		    jQuery(this).addClass('noborder_padding').find(".thumbwrap").removeClass("nojava_resize").animate(
			{
			    width: $imgwrap_x,
			    height: $imgwrap_y
			},
				transition_duration, transition_easing, function() {
				    var items = jQuery(".amplify_thumb").not(".feature_thumbs .amplify_thumb");
				    may_change = 1;
				    if (items.length > 3) {
				        items.slice(0, 1).remove();
				    }
				});
		});

        return false;
    }

}


function kriesi_mainmenu() {
    jQuery("#nav a").removeAttr('title');
    jQuery(" #nav ul ").css({ display: "none" }); // Opera Fix
    jQuery(" #nav li").hover(function() {
        jQuery(this).find('ul:first:hidden').css({ visibility: "visible", display: "none" }).fadeIn(500);
    }
		, function() {
		    jQuery(this).find('ul:first').css({ visibility: "hidden" });
		});
}




function kriesi_tab(wrapper, header, content) {
    var title = wrapper + " " + header;
    var container_to_hide = wrapper + " " + content;
    disable = false;


    jQuery(title).css({ display: "none" }).each(
					  function(i) {
					      if (i == 0) {
					          jQuery(wrapper).prepend("<a href='/' class='heading_tab advanced_link active tab" + (i + 1) + "'>" + jQuery(this).html() + "</a>");
					      } else {
					          jQuery(".advanced_link:last").after("<a href='/'class='heading_tab advanced_link tab" + (i + 1) + "'>" + jQuery(this).html() + "</a>");
					      }
					  }
					  );

    jQuery(container_to_hide).each(
						 function(i) {
						     jQuery(this).addClass("tablist list_" + i);
						     if (i != 0) {
						         jQuery(this).css({ display: "none" });
						     }
						 }
					 );

    jQuery(".advanced_link").each(
					  function(i) {
					      jQuery(this).bind("click", function() {
					          if (jQuery(this).hasClass('active')) { return false }
					          if (disable == false) {
					              disable = true;
					              jQuery(".advanced_link").removeClass("active");
					              jQuery(this).addClass("active");

					              /*adaptation for slowfade*/
					              jQuery(".content_right .slowfade_mod").removeClass("slowfade_mod");
					              jQuery(".content_right .slowfade").remove();
					              jQuery(".heading_tab").slowfade({ use_span_class: "slowfade", use_class: "slowfade_mod", add_inner_html: true });
					              /*end adaptation*/

					              jQuery(container_to_hide + ":visible").fadeOut(200, function() {



					                  jQuery(".list_" + i).fadeIn(200, function() { disable = false; });
					              });
					          }
					          return false;

					      });
					  }
					  );
}







function call_lightbox_clone(action) {
    if (action != "auto") {
        clearInterval(active_rotation);
    }
    jQuery("a[rel^='prettyPhoto'], a[rel^='lightbox']").not(".feature_thumbs .amplify_thumb a").prettyPhoto({ theme: 'dark_square', padding: 40 });
}



(function($) {
    $.fn.slowfade = function(default_options) {
        var defaults =
		{
		    duration: 400,
		    opacity: 1,
		    add_inner_html: false, 	//set true for text links
		    use_class: false, 		// set to classname
		    use_span_class: false, 	// set to classname
		    ie6comp: false 			//ie6 compatibility mode, used when ie6 is used and wrapping item has no fixed width, set to classname
		};
        var default_options = $.extend(defaults, default_options);

        return this.each(function() {
            var my = $(this);
            var $pos = my.css("position") == "absolute" ? "absolute" : "relative";
            var $bg = my.css("background-position") === undefined ? my.css("background-position-x") + ' ' + my.css("background-position-y") : my.css("background-position");
            var $inner_html = default_options.add_inner_html ? my.text() : "";

            if (!default_options.use_class) {
                my.css({ position: $pos, backgroundPosition: "right bottom" });
            }
            else {
                my.addClass(default_options.use_class);
            }

            if (!default_options.use_span_class) {
                $hover_replace = $('<span>' + $inner_html + '</span>').appendTo(my).css(
				{
				    height: my.height(), width: my.width(), backgroundImage: my.css("background-image"), backgroundPosition: $bg,
				    cursor: "pointer", position: "absolute", top: "0", left: "0", display: "block"
				});
            }
            else {
                $hover_replace = $('<span class="' + default_options.use_span_class + '">' + $inner_html + '</span>').appendTo(my);
            }

            if (default_options.ie6comp && $.browser.msie && $.browser.version.substr(0, 1) < 7) {
                $hover_replace.css({ height: my.height(), width: my.width() }).addClass(default_options.ie6comp);
            }


            $hover_replace.hover(
			function() {
			    $(this).stop().animate({ opacity: 0 }, default_options.duration);
			    if (default_options.opacity != 1) my.stop().animate({ opacity: default_options.opacity }, default_options.duration);
			},
			function() {
			    $(this).stop().animate({ opacity: 1 }, default_options.duration);
			    if (default_options.opacity != 1) my.stop().animate({ opacity: 1 }, default_options.duration);

			});
        });
    }
})(jQuery); 





