jQuery(document).ready(function(){
    var app = new Object();
    app.vLineHtml = '<div class="v-line"></div>';
    app.hLineHtml = '<div class="h-line"></div>';
    app.directionArray = new Array('left', 'right');
    app.randomNumber = function(m,n){
        m = parseInt(m);
        n = parseInt(n);
        return Math.floor( Math.random() * (n - m + 1) ) + m;
    }
    app.clickRow = function(direction){
        if (works.length < 8){return false;}
	    var id = jQuery('#works-list-index').find('li:first').attr('id');
	    id = id.substring(5);
	    var n_element = hash_table[id];
	    var result_html = '';
	    var i = 0;
	    var j = 0;
			
	    if(direction != 'right'){
		    if(typeof works[n_element - 1] != 'undefined'){n_element = n_element - 2;}
		    else{n_element = (works.length - 2);}
	    }
	    while(j != 7){
		    var act1 = '';
		    var act2 = '';
		    if(typeof works[n_element + (i+1)] != 'undefined'){
			    if(current_id == works[n_element + (i+1)][0]){
				    act1 = 'class="act"';
				    act2 = app.vLineHtml;
			    }
			    result_html = result_html+'<li '+act1+' id="w_id_'+works[n_element + (i+1)][0]+'">';
			    result_html = result_html+act2+'<a href="'+works[n_element + (i+1)][2]+'" rel="'+works[n_element + (i+1)][6]+'" title="'+works[n_element + (i+1)][3]+'">';
			    result_html = result_html+'<img src="'+works[n_element + (i+1)][5]+'" />';
			    result_html = result_html+'</a>';
			    result_html = result_html+'<span class="title" rel="'+works[n_element + (i+1)][1]+'">'+works[n_element + (i+1)][4]+'</span></li>';
			    i++;
			    j++;
		    }
		    else{
			    i = 0;
			    n_element = 0;
			    if(current_id == works[i][0]){
				    act1 = 'class="act"';
				    act2 = app.vLineHtml;
			    }
			    result_html = result_html+'<li '+act1+' id="w_id_'+works[i][0]+'">';
			    result_html = result_html+act2+'<a href="'+works[i][2]+'" rel="'+works[i][6]+'" title="'+works[i][3]+'">';
			    result_html = result_html+'<img src="'+works[i][5]+'" /></a>';
			    result_html = result_html+'<span class="title" rel="'+works[i][1]+'">'+works[i][4]+'</span></li>';
			    j++;
		    }
	    }
				
	    jQuery('#works-list-index').html(result_html);
	    tmp_current_id = current_id;
	    previewClick();			
	    current_id = tmp_current_id;
	    return false;
    };
    app.randWork = function(){
        var ids = new Array();
        jQuery('.nav-works[rel="right"]').trigger('click');
        jQuery('#works-list-index li').each(function(){
            ids.push(jQuery(this).attr('id'));
        });
        var indexWork = app.randomNumber(0, ids.length - 1);
        //alert(ids);
        var id = ids[indexWork];
        jQuery('#works-list-index #' + id +' a').trigger('click');
    };
    //Действия, выполняемые при загрузке страницы
	var current_id = big_photo[0];
	jQuery('#work-dn-index em').html(big_photo[2]+'<br />'+big_photo[3]);
	jQuery('#categories-list a[rel='+big_photo[1]+']').before(app.hLineHtml).parent().addClass('act');
	jQuery('#works-list-index li[id=w_id_'+big_photo[0]+']').addClass('act');
	jQuery('#row-left, #row-right').css('visibility', 'visible');
	jQuery('#works-list-index li[id=w_id_'+big_photo[0]+'] a').before(app.vLineHtml);
	//Действия, выполняемые при загрузке страницы
		
	var previewClick = function(){
	    jQuery('#works-list-index a').click(function(){
		    current_id = jQuery(this).parent().attr('id').substr(5);
		    jQuery('#works-list-index .act').removeClass('act');
		    jQuery(this).parent().addClass('act');
		    jQuery('#works-list-index div.v-line').remove();
		    jQuery(this).before(app.vLineHtml);
		    jQuery('#big-photo-link').attr('href', jQuery(this).attr('href')).attr('title', jQuery(this).attr('title'));
		    jQuery('#big-photo-link img').attr('src', jQuery(this).attr('rel')).attr('alt', jQuery(this).attr('title'));		
		    jQuery('#work-dn-index em').html(jQuery(this).parent().find('span').html());
		    jQuery('#categories-list .act').removeClass('act');
		    jQuery('div.h-line').remove();
		    jQuery('#categories-list a[rel='+jQuery(this).parent().find('span').attr('rel')+']').before(app.hLineHtml).parent().addClass('act');
		    return false;
	    });
	}
	previewClick();
	//Функция навигации по портфолио на главной
	jQuery('.nav-works').click(function(){
		return app.clickRow(jQuery(this).attr('rel'));
	});
	//app.randomNumber();
	
	var curInterval = setInterval(function(){app.randWork();}, 10000);
});
