$(function(){

    InitGallery();

//Favorite text
	if ($('div.photo-r a.more').length || $('div.contents a.more').length){
		var moreTrigger = $('div.photo-r a.more');
		var more = $('div.more');
		more.find('p:last').addClass('last');
		var hideTrigger = more.next('a.hide');
		hideTrigger.hide();
		more.hide();
		moreTrigger.click(function(){
			$(this).parent('p').next('div.more').slideDown(300,function(){
				more.css('display','inline');
				hideTrigger.fadeIn();
				moreTrigger.fadeOut();
			});
			return false;
		});
		hideTrigger.click(function(){
			$(this).hide().prev('div.more').slideUp(300,function(){
				moreTrigger.fadeIn();
			})
			return false;
		});
	}

//Slider
	var sliderWrap = $('div#slider_wrap'),
		next = $('div#slider_out').find('div.next'),
		prev = $('div#slider_out').find('div.prev'),
		slider = $('div#slider'),
		sliderItem = $('div#slider>div.photo-r'),
		firstItem = $('div#slider>div.photo-r:first'),
		lastItem = $('div#slider>div.photo-r:last'),
		speed = 1;
	if (sliderWrap.length) {
		firstItem.clone().appendTo(slider).addClass('first-cloned');
		lastItem.clone().prependTo(slider).addClass('last-cloned');
		firstItem.addClass('active');
		$(window).bind('load resize',function(){
			$('div#slider>div.photo-r').width(sliderWrap.width());
			slider.css('left',- sliderWrap.width()*slider.find('div.active').index());
		});
		next.click(function(){
			slider.animate({
				left: '-=' + sliderItem.width()
			},speed,function(){
				slider.find('div.active').removeClass('active').next().addClass('active');
				if (slider.children('div:last').hasClass('active')){
					firstItem.addClass('active').siblings().removeClass('active');
					slider.css('left',- sliderWrap.width()*slider.find('div.active').index());
				}
			});
		});
		prev.click(function(){
			slider.animate({
				left: '+=' + sliderItem.width()
			},speed,function(){
				slider.find('div.active').removeClass('active').prev().addClass('active');
				if (slider.children('div:first').hasClass('active')){
					lastItem.addClass('active').siblings().removeClass('active');
					slider.css('left',- sliderWrap.width()*slider.find('div.active').index());
				}
			});
		});
	}
//TABS
    InitAboutTabs();
	
//Form
	var orderForm = $('form#p_order');
	if (orderForm.length){
		var defVal = ['Ваше имя','Телефон','E-mail','Комментарий'],
			inputs = orderForm.find('.change');
		inputs.focus(function(){
			for (i=0;i<defVal.length;i++) {
				if (this.value == defVal[i]) {
					$(this).val('').css('color','#000');
				}				
			}
		});
		inputs.blur(function(){
			for (i=0;i<defVal.length;i++) {
				if (this.value == '') {
					$(this).val(defVal[$(this).index('.change')]).css('color','#A3A3A3');
				}
			}
		});
	}
});



function InitGallery() {
    
    var start_index = 0;
    if (!isFavoritesPage) {
        if (window.location.hash.match(/^\#image_\d+$/)) {
            start_index = $('.ad-thumb-list a').index($('a[rel='+window.location.hash+']'));
        }
    }
    
    //Gallery
	var gallery = $('div.ad-gallery');
	if (gallery.length){
		$('.ad-gallery').adGallery({
			display_next_and_prev: true,
			effect: 'none',
			slideshow:{
				enable : false
			},
			loader_image : '/img/loader.gif',
            start_at_index : start_index,
			callbacks:{
				afterImageVisible: function(){
					var gallery = $('div.ad-gallery');
					var currentPhoto = $('ul.ad-thumb-list a.ad-active img');
					var imgContainer = $('div.ad-image');
					if (currentPhoto.attr('link') && currentPhoto.attr('name')){
						imgContainer.append('<div class="author"><span>Вид съёмок: </span><a href="#"></a></div>')
						var author = $('div.author a');
						author.attr('href',currentPhoto.attr('link'))
						.html(currentPhoto.attr('name'));
					}
					if (imgContainer.find('img').width() > 800){
						imgContainer.css({
							width: '100%',
							position: 'static',
							display: 'inline-block',
							height:'auto',
							left: 0,
							top:0
						});
						imgContainer.parent().css({
							height:'auto',
							padding: 0,
							margin: '-5px 0 0 0'
						});
						imgContainer.find('img').removeAttr('width').removeAttr('height').css({
							width:'99.8%'
						});
					}
					else {
						imgContainer.parent().css({
							height:'400px',
							position:'relative',
							paddingBottom:'30px',
							marginBottom:'15px'
						});
					}
					imgContainer.append('<a href="#" class="fav"></a>');
			
					//if img is small
					imgContainer.width()<410 ? gallery.addClass('small_img') : gallery.removeClass('small_img');
					
					//check if image is in favoriets
                    var imgId = currentPhoto.attr('fav');
					var favIco = gallery.find('a.fav');
                    $.inArray(imgId,Favs) == -1 ? gallery.removeClass('has_favorite') : gallery.addClass('has_favorite');
                    gallery.hasClass('has_favorite') ? favIco.attr('title','Удалить из избранного') : favIco.attr('title','Добавить в избранное');
                    
                    // update hyash
                    window.location.hash = 'image_'+imgId;
                    // LOADS SOCIAL LIKE BUTTONS
                    LoadLikeButtons();
					
					$('div.ad-nav').width($('div.ad-thumbs ul.ad-thumb-list').width());//fix width
				}
			}		
		});
	};
    
}


function InitAboutTabs() {
    var photoNav = $('ul#photo_nav');
	if (photoNav.length) {
		var tabContainer = photoNav.parents('div#content').find('div.g_wrap').find('div.content'),
			activeTab = photoNav.find('li.active a').attr('href');
			
 			tabContainer.children('div').addClass('hidden');
			tabContainer.children('div#'+activeTab).addClass('active').removeClass('hidden').siblings().removeClass('active').addClass('hidden');
			photoNav.find('li a').click(function(){
				if(!$(this).parent().hasClass('active')){
					$(this).parent().addClass('active').siblings().removeClass('active');
					tabContainer.children('div#'+$(this).attr('href')).addClass('active').removeClass('hidden')
					.siblings().removeClass('active').addClass('hidden');
					
					return false;
				}
				return false;
			});

	}
    
}
