function anchor() //Gets anchor part from current URL and splits it to array. String #a#b#c represents as array = ['a','b','c'].
{
	return document.location.toString().substr(document.location.toString().indexOf('#')).split('#');
}

function hide_slides(speed) //Hide all slides
{
    $('.slideshow-wrapper div.slide').queue(function(){
	$(this).hide(speed).dequeue();
    });
}

function get_current_slide() //Get current slide ID from anchor part of URL
{
    for (i in an) 
	if (strstr(an[i],'slide_',false)&&$('#'+an[i]).length>0) //If anchor contains string "slide_\w" (where \w is string variable that contains slides name) and div with this slide name exists, set current_slide to its value.
	    current_slide=an[i];
    if (current_slide==0||current_slide=='') current_slide=$('.slideshow-wrapper div.slide:first').attr('id'); //If no slide_ variables found in anchor, and current_slide value is 0 or undefined, set current_slide variable to first slides id.
    return current_slide;
}

function show_slide(slide_id,speed) //Show slide by its div ID
{
    var str='';
    if (slide_id==0||$('.slideshow-wrapper div[id="'+slide_id+'"]').length==0)
    str='.slideshow-wrapper div.slide:first'; else //If slide_id = 0 or div with id equal to slide_id doesn't exist then show first slide
    str='.slideshow-wrapper div[id="'+slide_id+'"]'; //Otherwise show slide with id equal to slide_id
    $(str).queue(function(){
	$(this).show(speed).dequeue();
    });
}

function hide_slide(slide_id,speed) //Show slide by its div ID
{
    if (slide_id==0||$('.slideshow-wrapper div[id="'+slide_id+'"]').length==0) 
	$('.slideshow-wrapper div.slide:first').hide(speed); //If slide_id = 0 or div with id equal to slide_id doesn't exist then show first slide
    else
	$('.slideshow-wrapper div[id="'+slide_id+'"]').hide(speed); //Otherwise show slice with id equal to slide_id
}

function get_max_height() //Get maximum height from slides
{
    var max_height=0;
    $('.slideshow-wrapper div.slide').each(function(){
	if ($(this).height()>max_height) max_height=$(this).height();
    });
    return max_height;
}

function show_nav_arrows() //Shows navigation arrows
{
    $('.slideshow-wrapper div.slideshow-container').before('<div class="slide_nav_arrow_left" />').after('<div class="slide_nav_arrow_right" />'); //Wrap container div with arrow divs
    $('.slideshow-wrapper div.slide_nav_arrow_left').html('<a href="#" class="slideshow-nav-left"><img src="/images/menu_left_i.gif"/></a>'); //Set left arrow div content
    $('.slideshow-wrapper div.slide_nav_arrow_right').html('<a href="#" class="slideshow-nav-right"><img src="/images/menu_right_i.gif"/></a>'); //Set right arrow div content
    var ml = $('.slideshow-wrapper div.slide_nav_arrow_left').width(); //Find margin(left arrow width) for container and right arrow div
    var mr = $('.slideshow-wrapper div.slide_nav_arrow_right').width(); //Find margin for right arrow div (its width)
    $('.slideshow-wrapper .slideshow-container').css({'margin-left':-ml+'px','padding-right':mr+'px','height':get_max_height()}); //Set margin and height to container
    $('.slideshow-wrapper div.slide_nav_arrow_right').css({'left':-ml-mr+'px'}); //Set margin for left arrow
    $('a.slideshow-nav-left').click(function(){ //If left navigation arrow is clicked
	new_slide=$('#'+current_slide).prev().attr('id'); //Try to find previous slide
	if (!new_slide) new_slide=$('.slideshow-wrapper div.slide:last').attr('id'); //If previous slide not found then get last one
	current_slide=new_slide; //Set current silde variable to new value
	window.location.hash=new_slide; //Set anchor to new value
	return false; //Prevent browser from handling this click event
    }).hover(function(){ //Toggle left arrows behavoir
	$(this).children('img').attr('src','/images/menu_left_a.gif'); //On mouse over show "active" image
    },function(){
	$(this).children('img').attr('src','/images/menu_left_i.gif'); //On mouse out show "inactive" image
    });
    $('a.slideshow-nav-right').click(function(){//If right navigation arrow is clicked
	new_slide=$('#'+current_slide).next().attr('id'); //Try to find next slide
	if (!new_slide) new_slide=$('.slideshow-wrapper div.slide:first').attr('id'); //If next slide not fount then get first one
	current_slide=new_slide;
	window.location.hash=new_slide;
	return false;
    }).hover(function(){//Toggle right arrows behavoir
	$(this).children('img').attr('src','/images/menu_right_a.gif');
    },function(){
	$(this).children('img').attr('src','/images/menu_right_i.gif');
    });
}

function show_nav_list() //Show navigation panel
{
    var name=''; //Will contain slides name
    var str='<div class="slideshow_nav_list">ûÁÇÉ:&nbsp;'; //Open navigation panel div
    var i=0;
    var cc=''; //Will be used to mark currently shown slides name
    $('.slideshow-wrapper div.slide').each(function(){ //Get each slide
	id=$(this).attr('id'); //Get slides id
	name=id.slice('slide_'.length); //Get slides name, substracting it from slides id
	if (i>0) str+=',&nbsp;'; //If not first slide, add a divider
	i++;

	if (id==current_slide) cc=' class="current"'; else cc=''; //If current slides id equal to currently shown slide id then mark its name.
	str+='<a href="#'+id+'"'+cc+'>'+name+'</a>'; //Add a slide link with its name as a link text to the navigation menu
    });
    str+='</div>'; //Close navigation panel div
    $('.slideshow-wrapper').append('<br/>'+str); //Apply changes to .slideshow-wrapper div
    $('.slideshow-wrapper .slideshow_nav_list a').click(function(){ //Handle clicks on navigation panel lings
	new_slide=$(this).attr('href').slice(1); //Get new slides id from navigation links href value
	window.location.hash=new_slide; //Set new anchor value
	return false; //Prevent browser from handling this click event
    });

}

function change_slide() //Changes slide if its id found in anchor variables array
{
    an=anchor(); //Get variables from anchor
    for (i in an) //Seek in anchor variables array
    if (strstr(an[i],'slide_',false)) //If "slide_" variable is found
    {
	hide_slides(); //Hide all slides
	new_slide=get_current_slide(); //Get new slides id from anchor
	show_slide(new_slide,'slow'); //Show new slide
	$('.slideshow-wrapper .slideshow_nav_list a').removeClass('current'); //Remove "current" mark from navigation panel
	$('.slideshow-wrapper .slideshow_nav_list a[href="#'+new_slide+'"]').addClass('current'); //Add "current" mark to new slide'ss name in navigation panel
    }
}

var an=anchor(); //Anchor array
var current_slide=0; //Holds an ID of a div that is the current shown slide.
var new_slide=''; //Holds new slides id when anchor is changed

$(window).bind('hashchange',function(){ //Handle window hash change event
    change_slide(); //Try to change current slide
});

$(window).load(function(){ //When whole page is loaded
    hide_slides(); //Hide all slides
    change_slide(); //Try to find slide to be shown
    if (new_slide=='') //If failed
    {
	show_slide(0,'fast'); //Show first slide
	current_slide=$('.slideshow-wrapper div.slide:first').attr('id'); //Set current_slide variable to first slides id
    }
    show_nav_arrows(); //Show navigation arrows
    show_nav_list(); //Show navigation panel
});
