/* -------------------------------------------------- *
 * Project scripts
 * -------------------------------------------------- *
 * Author: Morozov Igor
 * URL: http://www.morozoff.info/
 * Copyright: 2010 Morozov Igor
** -------------------------------------------------- */

function array_remove(array, value) {
    new_array = [];

    for (var i = 0; i < array.length; i++) {
        if (e[i] != value) new_array.push(e[i]);
    }

    return new_array;

}

Array.prototype.inArray = function (value) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == value) {
            return true;
        }
    }
    return false;
};

function update_fav_menu() {
    e = ($.cookie('fav') || '').split(',');

    if (e.length == 0 || (e[0] < 1 && e.length == 1)) {
        $('.fav').hide();
    } else {
        $('.fav big').text(e.length - 1);
        $('.fav').show();
    }
}

function fav(id) {
    e = ($.cookie('fav') || '').split(',');
    _in = false;

    if (e.inArray(id)) {
        e = array_remove(e, id);
        $('.to-fav a').text('В избранное');
        _in = false;
    } else {
        e.push(id);
        $('.to-fav a').text('Отобрано');
        _in = true;
    }

    if (_in && !$('.to-fav').hasClass('to-fav-active')) {
        $('.to-fav').addClass('to-fav-active');
    } else if (!_in && $('.to-fav').hasClass('to-fav-active')) {
        $('.to-fav').removeClass('to-fav-active');
    }

    $.cookie('fav', cookie_val(e), { path: '/', expires: 0 });

    update_fav_menu();
}

function fav_del(id) {
    e = ($.cookie('fav') || '').split(',');
    e = array_remove(e, id);

    $.cookie('fav', cookie_val(e), { path: '/', expires: 0 });
    update_fav_menu();
    $('#p' + id).slideUp(100, function () {
        $('#p' + id).remove();
    });
}

function cookie_val(e) {
    if (e.length == 0 || (e.length == 1 && e[0] == '')) {
        return '';
    } else {
        return e.join(',');
    }
}

$(document).ready(function() {
	if($('#slider-vis').length) {
		$("#slider-vis .b-slider-i").scrollable({
			next: $('#slider-vis .ar-r'),
			prev: $('#slider-vis .ar-l'),
			circular: true
		});
		$("#slider-vis .ar").click(function(){
			return false;
		})
	}
	if($('#gallery-nav').length) {
		var $gal = $('#gallery-nav');
		if ($('#gallery-nav .b-gallery-i li').length < 2) {
			$('#gallery-nav .numb-i').hide();
		}
		$(".b-gallery-i",$gal).scrollable({
			next: $('.ar-r',$gal),
			prev: $('.ar-l',$gal),
			circular: true,
			onSeek: function() {
				$('.curr-numb',$gal).html(this.getIndex()+1)
			}
		});
		var $api = $(".b-gallery-i",$gal).data("scrollable");

		$('.tot-numb',$gal).html($api.getSize())

		$(".ar",$gal).click(function(){
			return false;
		})
	}
	if($('.catalog-page').length) {
		var $item = $('.catalog-page');
		$(".wrapitems",$item).scrollable({
			next: $('.ar-r',$item),
			prev: $('.ar-l',$item),
			items: '.b-items'
			//circular: true
		}).navigator({indexed:true});
		//var $api = $(".b-gallery-i",$gal).data("scrollable");

		//$('.tot-numb',$gal).html($api.getSize())

		$(".ar",$item).click(function(){
			return false;
		})
	};
	$(".lightZoom").fancybox({
		'titleShow'		: false
	});
	$('.lightZoom_go').bind('click', function() {
		$(".lightZoom:first").trigger('click');
		return false;
	});

	$(".catalog-panorama-link a").fancybox({
		'titleShow'		: false
	});


	$("div.contacts h4").wrapInner('<span />');
    $("div.contacts h4").next("ul").hide();
    $('div.contacts h4').bind('click', function() {
        $(this).next('ul').toggle();
        $(this).toggleClass('active');
        return false;
    });

});
