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

$(function(){
	if($(".visarea__ul").length) {
		$(".visarea__ul").carouFredSel({
			circular: false,
			infinite: false,
			width: 940,
			height: 342,
			items: {
				visible: 1,
				width: 940,
				height: 342
			},
			scroll: {
				items: 1,
				easing: "linear",
				onAfter:function(oldItems,newItems) {
					//	get the current position using the callback function
					$(".visarea__ul").trigger("currentPosition", function( pos ) {
						$('.b-visnav a').removeClass('active').eq(pos).addClass('active')
					});
					
				}
			},
			auto: false
		});	
		$(".visarea__ul").trigger("linkAnchors", [".b-visnav", ".it"]);
	};
	if($('.b-linegallery').length) {
		linegallery();
	};
	
	if($('.galleryseries-items').length) {
		$('.galleryseries-items').each(function(){
			var $el = $(this),
				$el_scroll = $('.galleryseries-area',$el),
				$el_scroll_hgh = $('ul',$el_scroll).height();
				$el_scroll.scrollTop(0)

			$('.ar-l',$el).bind('click', function(){
				$el_scroll.scrollTo( '-=272px', 500, {
					axis:'y',
					onAfter:function(){
						checkarrows();
					}
				});
				return false;
			});

			$('.ar-r',$el).bind('click', function(){
				$el_scroll.scrollTo( '+=272px', 500, {
					axis:'y',
					onAfter:function(){
						checkarrows();
					}
				});
				return false;
			});

			function checkarrows() {
				if($el_scroll_hgh <= 272) {
					$('.ar-r',$el).addClass('disabled');
					$('.ar-l',$el).addClass('disabled');
				}
				else if($el_scroll.scrollTop() === 0) {
					$('.ar-l',$el).addClass('disabled');
					$('.ar-r',$el).removeClass('disabled');
				}
				else if(parseInt($el_scroll_hgh - 272) <= $el_scroll.scrollTop()) {
					$('.ar-r',$el).addClass('disabled');
					$('.ar-l',$el).removeClass('disabled');

				}
				else {
					$('.ar-r',$el).removeClass('disabled');
					$('.ar-l',$el).removeClass('disabled');
				}

			}

			checkarrows();

		});
	};
	
	if($('.b-dealer-enter').length) {
		$('.b-dealer-enter .i-p').toggleVal({
			populateFrom: 'title'
		});
		$('.b-dealer-enter .close').click(function(){
			$('.b-dealer-enter').toggleClass('active-dealer-enter');
			return false;
		});
	};
		
	$('.feedback-dealer-link').live('click', function(){
		$(this).parent().toggleClass('feedback-dealer-active');
		return false;
	});
	$('.li-feedback-dealer .close').live('click', function(){
		$(this).parents('li').removeClass('feedback-dealer-active');
		return false;
	});
	
	$('.personalcons-widget .armore').bind('click', function(){
		$(this).parent().toggleClass('personalcons-hide')
		return false;
	});
	
	if($('.b-prodscollection-dealer').length) {
		$('.b-prodscollection-dealer').each(function(){
			var $t = $(this),
				p = $('.ar-l',$t),
				n = $('.ar-r',$t);
			$('.prods-area-scroll__ul',$t).carouFredSel({
				circular: false,
				infinite: false,
				padding:false,
				align:false,
				items: {
					visible: 5
				},
				scroll: {
					items: 3
				},
				auto: false,
				prev: p,
				next: n
			});
		});
	}
	
	if($('.b-catmedia').length) {
		productpage();
	};
	
	if($('.gallery-items-alone').length) {
		galleryitems();
	};
	
	if($('.gallery-items-dealer').length) {
		galleryshow();
	};
	
	if($('.pp-subnav').length) {
		$('.pp-subnav .b-subnav .close, .hdnav__h3').click(function(){
			$(this).parents('.b-subnav').toggleClass('active-ppsubnav')
			return false;
		});
	}
	
	$('.dealer-map-link').click(function(){
		$('.dealer-map').toggleClass('dealer-map-active');
		return false;
	});
	
	/* $(".galleryseries-area__ul").carouFredSel({
		direction: "up",
		circular: false,
		infinite: false,
		width: 405,
		height: 240,
		items: {
			visible: 6,
			minimum: 6,
			width: 135,
			height: 120
		},
		scroll: 3,
		auto: false
	}); */
});

function linegallery(){
	$('.b-linegallery').not('.nlinegallery').each(function(){
		var $gal = $(this),
			$thumblink = $gal.next().find('.all-gallery-link'),
			txtShow = $thumblink.find('span').text(),
			txtHide = $thumblink.attr('rel'),
			numbShowItems = $('.open',$gal).length,
			totItems = $('li',$gal);

		$thumblink.click(function(){
			var $t = $(this);
			if($t.hasClass('active-thumblink')) {
				$t.removeClass('active-thumblink');
				$('span',$t).text(txtShow);
				totItems.removeClass('open').slice(0,numbShowItems).addClass('open')
			}
			else {
				$t.addClass('active-thumblink');
				$('span',$t).text(txtHide);
				totItems.addClass('open');
			}
			return false;
		});
		
	});
	
}

function productpage() {
	var $catmedia = $('.b-catmedia');
	
	if($('#gallery-area').length) {
		galleryshow();
	};
	
	if($('.b-cat-desc').length)	{
		$('.b-cat-desc .tab-link').click(function(){
			var $t = $(this);
			if(!$t.hasClass('tab-active')) {
				$('.b-cat-desc .tab-link').removeClass('tab-active');
				$('.panes-cattxt .pane').hide();
				$t.addClass('tab-active');
				$($t.attr('href')).show();
			};
			return false;
		});
	}
	
	if($(".spec-area li").length > 4) {
	
		$(".spec-area ul").carouFredSel({
			circular: false,
			infinite: false,
			width: 139,
			align: false,
			scroll: 1,
			auto: false,
			items: {
			    visible: 4
			},
			prev: ".spec-scroll .ar-l",
			next: ".spec-scroll .ar-r"
		});
	
	}
	else {
		if($('.col-spec').hasClass('spec-type-1')) {
			$('.col-spec').removeClass('spec-type-1').addClass('spec-type-2')
		}
	}

	$('.collection-items .r-title-i').click(function(){
		var $par = $(this).parent().parent();
		if($par.hasClass('collection-activeitems')) {
			$par.removeClass('collection-activeitems');
		}
		else {
			var $w = $(this).width();
			$par.addClass('collection-activeitems');
			$('.b-coll-series',$par).css('width',$w+'px');
		}
		return false;
	});
	
	$('.latest-views-area__div').carouFredSel({
		circular: false,
		infinite: false,
		width: 130,
		items: {
			visible: 1
		},
		scroll: {
			items: 1
		},
		height:'auto',
		auto: false,
		prev: '.b-latest-views .prev-ar',
		next: '.b-latest-views .next-ar'
	});
	
	$('.b-prods-area').each(function(){
		var $t = $(this),
			p = $('.ar-l',$t),
			n = $('.ar-r',$t);
		$('.prods-area-scroll__ul',$t).carouFredSel({
			circular: false,
			infinite: false,
			padding:false,
			align:false,
			items: {
				visible: 6
			},
			scroll: {
				items: 3
			},
			auto: false,
			prev: p,
			next: n
		});
	});
	
	if($('.b-prodscollection .b-prods-area .prods-area-scroll__ul li.active').length){
		$('.b-prodscollection .b-prods-area .prods-area-scroll__ul').each(function(){
			$(this).trigger("slideTo", $(this).find('li.active'));
		});
	}
	
	$('.color-chs-sel').live('mouseenter', function(){
		$(this).parent().addClass('active-color-chs');
	});

	$('.color-chs-sel a').live('click', function(){
		return false;
	});
	
	$('.color-chs-expand a').live('click', function(){
		$(this).parents('.color-chs-expand').find('li').removeClass('selected');
		$(this).parent().addClass('selected');
		$(this).parents('.color-chs').find('.color-chs-sel a span').attr({
			style: $(this).attr('style')
		}).parent().attr({
			rel: $(this).attr('rel')
		});
		return false;
	});
	
	$('.color-chs').live('mouseleave', function(){
		$(this).removeClass('active-color-chs');
	});
	
	$('.b-media-modes .view-3d').click(function(){
		$('#overlay,#pp-gallery-3d').show();
		$('body').addClass('pp-gallery-show');
	});
	
	$('#overlay, #close, #pp-gallery-3d .close').click(function(){
		$('body').removeClass('pp-gallery-show');
		$('#overlay,#pp-gallery-fsc,#pp-gallery-3d').hide();
		return false;
	});
	
};

function galleryshow(){

	var $galleryarea = $('#gallery-area'),
			$galleryarea__line = $('#gallery-area-line'),
			$thumblink = $('.all-gallery-link',$galleryarea__line),
			irel,
			txtShow = $thumblink.find('span').text(),
			txtHide = $thumblink.attr('rel'),
			ultra__thumb = '',
			ultra__photo = '',
			cnt_txt = '',
			ratio = 1.5;



		$galleryarea.scrollLeft(0);
	
	// build big gallery
	
	$('<div id="pp-gallery-big"><div id="pp-gallery-big-i"><div id="pp-gallery-photos"><ul></ul></div><div id="pp-gallery-txt"><div class="txt"></div></div></div></div> <div id="pp-gallery-thumb"><div class="ar-t"><a /></div><div id="pp-gallery-thumb-area"><ul /></div><div class="ar-b"><a /></div></div>').appendTo('#pp-gallery-fsc');
	if($('#gallery-area-txt').html().length > 0) {
		cnt_txt = $('#gallery-area-txt').html();
		$('#pp-gallery-txt .txt').html(cnt_txt)
	}
	else {
		$('#pp-gallery-txt .txt').hide();
	}
	
	$('<div id="imageZoom" />').appendTo('body')
	
	$('#gallery-chs-colors').clone().attr('id','pp-chs-colors').insertBefore('#pp-gallery-txt .txt');
	
	var $el = $('#pp-gallery-thumb'),
		$el_scroll = $('#pp-gallery-thumb-area'),
		$el_scroll_hgh;
		$el_scroll.scrollTop(0)
		
	if($('.linegallery__ul a[data-ultraphoto]').length > 0) {
	
		$('.linegallery__ul li').each(function(){
			var $t = $(this),
				$t__cls = $t.attr('class');
			
			if($('a',$t).data('ultraphoto')) {
				ultra__thumb += '<li class="'+ $t__cls +'" data-photo-count='+ $('a',$t).data('photo-count') + '><a href='+$('a',$t).data('ultraphoto')+'><img src='+ $('img',$t).attr('src') +' /></a></li>';
				if($('a',$t).data('ultrabigphoto')) {
					ultra__photo += '<li class="'+ $t__cls +'" data-photo-count='+ $('a',$t).data('photo-count') + '><a href='+$('a',$t).data('ultrabigphoto')+'><img src='+ $('a',$t).data('ultraphoto') +' /><i class="zoom"></i></a></li>';
				}
				else {
					ultra__photo += '<li class="'+ $t__cls +'" data-photo-count='+ $('a',$t).data('photo-count') + '><img src='+ $('a',$t).data('ultraphoto') +' /></li>';
				}
			}
			
			
		});
		
		$('#pp-gallery-photos ul').html(ultra__photo);
		$('#pp-gallery-thumb-area ul').html(ultra__thumb);
		
	}
	else {
		$('.b-media-modes .fullscreen').parent().hide();
	}
	
	// thumb small gallery
	
	$('.linegallery__ul a',$galleryarea__line).click(function(){
	
		if(!$(this).hasClass('active')) {
			$('.linegallery__ul a',$galleryarea__line).removeClass('active');
			$(this).addClass('active')
			var idx = $('.linegallery__ul .'+irel+' a',$galleryarea__line).index(this);

			$galleryarea.scrollTo( $galleryarea.find('li.'+irel+':eq('+ idx+ ')'), 800, {
				axis:'x'
			});
		}
		return false;
	});
	
	// color small gallery
	
	if($('#gallery-chs-colors').length) {
		$('#gallery-chs-colors a').click(function(){
			if(!$(this).hasClass('active')) {
				var $t = $(this),
					idx = 0;
				
				if(irel) {
					idx = $('.linegallery__ul .'+irel+' a',$galleryarea__line).index($('.linegallery__ul .'+irel+' .active',$galleryarea__line));
				}
				
				irel = $t.attr('rel');
				$('#gallery-chs-colors li').removeClass('active');
				$t.parent().addClass('active');
				
				$('li',$galleryarea).hide();
				$('li.'+irel,$galleryarea).show();
				
				//$galleryarea.scrollLeft(0);
				
				$('.linegallery__ul li',$galleryarea__line).removeClass('open')
				$('.linegallery__ul .'+irel,$galleryarea__line).slice(0, 5).addClass('open')
				$('.linegallery__ul .'+irel,$galleryarea__line).eq(idx).find('a').trigger('click');
				
				$thumblink.removeClass('active-thumblink');
				$('span',$thumblink).text(txtShow);
				$('.linegallery__ul li',$galleryarea__line).removeClass('open')
				$('.linegallery__ul .'+irel,$galleryarea__line).slice(0,5).addClass('open')
				
				if($('.linegallery__ul .'+irel,$galleryarea__line).length < 6) {
					$thumblink.parent().css('visibility', 'hidden')
				}
				else {
					$thumblink.parent().css('visibility', 'visible')
				}
				
			}
			return false;
		}).eq(0).trigger('click');
	}
	else {
		irel = 'gallery-csh-1';
		$('li.'+irel,$galleryarea).show();
		$('.linegallery__ul .'+irel,$galleryarea__line).slice(0, 5).addClass('open')
		$('.linegallery__ul .'+irel,$galleryarea__line).eq(0).find('a').trigger('click');
		$('.linegallery__ul .'+irel,$galleryarea__line).slice(0,5).addClass('open');
		if($('.linegallery__ul .'+irel,$galleryarea__line).length < 6) {
			$thumblink.parent().css('visibility', 'hidden')
		}
		else {
			$thumblink.parent().css('visibility', 'visible')
		}
	}
	
	//gallery-area
	$('.b-media-modes .fullscreen').click(function(){
		var idx = $('.linegallery__ul a',$galleryarea__line).index($('.linegallery__ul .active',$galleryarea__line));
		$('#gallery-area a').eq(idx).trigger('click')
		return false;
	});
	$('#gallery-area a').click(function(){
	
		var $pp__height = $(window).height() - 80;
		
		if($pp__height < 531) {
			$pp__height = 530
		}
		
		if($pp__height > 759) {
			$pp__height = 760
		}
		
		var $pp__width = $pp__height * ratio;
		
		if(($pp__width + 145) > $(window).width()) {
			var diff = ($pp__width + 145) - $(window).width();
			
			$pp__width = $pp__width - diff
			
		}
		else {
			var diff = 0;
		}
		
		if($pp__width > 1139) {
			$pp__height = 760;
			$pp__width = $pp__height * ratio;
		}

		$('#pp-gallery-fsc').css({
			width:($pp__width - diff) + 145 + 'px',
			marginLeft: '-'+($pp__width - diff + 145)/2 +'px'
		})
		
		$('#pp-gallery-photos').height($pp__height - diff);

		$('#overlay,#pp-gallery-fsc').show();
		$('body').addClass('pp-gallery-show');
		
		$('#pp-gallery-photos').scrollLeft(0);
		
		$('#pp-gallery-photos li').hide();
		$('#pp-gallery-photos li.'+irel).show();
				
		$('#pp-gallery-thumb-area li').hide();
		$('#pp-gallery-thumb-area li.'+irel).show();
		
		$('#pp-chs-colors li').removeClass('active');
		$('#pp-chs-colors a[rel='+ irel +']').parent().addClass('active');
		
		var dph = $(this).data('photo-count'),
			dphscroll = $('#pp-gallery-photos li[data-photo-count='+dph+']');

		$('#pp-gallery-photos').scrollTo( dphscroll, 800, {
			axis:'x'
		});
		
		$el_scroll_hgh = $('ul',$el_scroll).height();
		
		$el_scroll.scrollTo('li[data-photo-count='+dph+']', 1, {
			axis:'y',
			onAfter:function(){
				checkgalar();
			}
		});
		
		$('#pp-gallery-thumb-area a').removeClass('active');
		$('#pp-gallery-thumb-area li[data-photo-count='+dph+'] a').addClass('active');
		
		return false;
	});
	
    $('#pp-gallery-photos a').live('click',function(e){
    	$('#imageZoom').show();
    	$('<img src='+ $(this).attr('href')+' id="imagezoom__img" />').appendTo('#imageZoom');
    	
    	$('#imagezoom__img').bind('load',function(){
    		var div = $('#imageZoom'),
    			ig = $('#imagezoom__img'),
    			divWidth = div.width(),
    			divHeight = div.height(),
    			igW = ig.width(),
    			igH = ig.height(),
    			dOs = div.offset(),
    			leftPan = (e.pageX - dOs.left) * (divWidth - igW) / (divWidth),
    			topPan = (e.pageY - dOs.top) * (divHeight - igH) / (divHeight);
    			ig.css({left: leftPan, top: topPan});
    	})
    	return false;
    });
    
    
    $('.ar-t a',$el).bind('click', function(){
    	$el_scroll.scrollTo( '-=240px', 500, {
    		axis:'y',
    		onAfter:function(){
    			checkgalar();
    		}
    	});
    	return false;
    });
    
    $('.ar-b a',$el).bind('click', function(){
    	$el_scroll.scrollTo( '+=240px', 500, {
    		axis:'y',
    		onAfter:function(){
    			checkgalar();
    		}
    	});
    	return false;
    });
    
    function checkgalar() {

    	if($el_scroll_hgh <= 480) {
    		$('.ar-t a',$el).addClass('disabled');
    		$('.ar-b a',$el).addClass('disabled');
    	}
    	else if($el_scroll.scrollTop() == 0) {
    		$('.ar-t a',$el).addClass('disabled');
    		$('.ar-b a',$el).removeClass('disabled');
    	}
    	else if(parseInt($el_scroll_hgh - 480) <= $el_scroll.scrollTop()) {
    		$('.ar-t a',$el).removeClass('disabled');
    		$('.ar-b a',$el).addClass('disabled');
    	}
    	else {
    		$('.ar-t a',$el).removeClass('disabled');
    		$('.ar-b a',$el).removeClass('disabled');
    	}
    };

	$('#pp-chs-colors a').click(function(){
		if(!$(this).hasClass('active')) {
			var $t = $(this);
			
			var idx = $('#pp-gallery-thumb-area li.'+irel+' a').index($('#pp-gallery-thumb-area li.'+irel+' .active'));
			
			irel = $t.attr('rel');
			$('#pp-chs-colors li').removeClass('active');
			$t.parent().addClass('active');
			
			$('#pp-gallery-photos li').hide();
			$('#pp-gallery-photos li.'+irel).show();
			
			$('#pp-gallery-thumb-area li').hide();
			$('#pp-gallery-thumb-area li.'+irel).show();
			
			$('#pp-gallery-thumb-area li.'+irel+' a').eq(idx).trigger('click');
			
			$el_scroll_hgh = $('ul',$el_scroll).height();
			checkgalar()
			
		}
		return false;
	});
	
	$('#pp-gallery-thumb-area a').click(function(){
		
		var dph = $(this).parent().data('photo-count'),
			dphscroll = $('#pp-gallery-photos li[data-photo-count='+dph+']');
		
		$('#pp-gallery-thumb-area a').removeClass('active');
		$('#pp-gallery-thumb-area li[data-photo-count='+dph+'] a').addClass('active');

		$('#pp-gallery-photos').scrollTo( dphscroll, 800, {
			axis:'x'
		});
		
		return false;
	});
	
	// thumblink small gallery
	
	$thumblink.click(function(){
		var $t = $thumblink;
		if($t.hasClass('active-thumblink')) {
			$t.removeClass('active-thumblink');
			$('span',$t).text(txtShow);
			$('.linegallery__ul li',$galleryarea__line).removeClass('open')
			$('.linegallery__ul .'+irel,$galleryarea__line).slice(0,5).addClass('open')
		}
		else {
			$('.linegallery__ul .'+irel,$galleryarea__line).addClass('open')
			$t.addClass('active-thumblink');
			$('span',$t).text(txtHide);
		}
		return false;
	});
	
	$("#imageZoom").miniZoomPan({
	
        sW: $(window).width(),

        sH: $(window).height(),

        lW: 2400,

        lH: 1600,

        loaderContent: "<span class=loader />",

        callback: function () {
//            scaleImage();
        }

    });
    
    function scaleImage() {
    	
    };
	
	$('#overlay, #close').click(function(){
		$('body').removeClass('pp-gallery-show');
		$('#overlay,#pp-gallery-fsc').hide();
		return false;
	});

	
}

function galleryitems(){
	var irel,
		cnt_txt = '',
		ratio = 1.5;
	
	if($('.gallery-items').length) {
		$('.gallery-items').each(function(){
			var $el_it = $(this),
				$el_it_scroll = $('.galleryseries-area',$el_it),
				$el_it_scroll_hgh = $('ul',$el_it_scroll).height();
				$el_it_scroll.scrollTop(0)

			$('.ar-l',$el_it).bind('click', function(){
				$el_it_scroll.scrollTo( '-=180px', 500, {
					axis:'y',
					onAfter:function(){
						checkarrows();
					}
				});
				return false;
			});
			
			$('.ar-r',$el_it).bind('click', function(){
				$el_it_scroll.scrollTo( '+=180px', 500, {
					axis:'y',
					onAfter:function(){
						checkarrows();
					}
				});
				return false;
			});
			
			function checkarrows() {
				if($el_it_scroll_hgh <= 180) {
					$('.ar-r',$el_it).addClass('disabled');
					$('.ar-l',$el_it).addClass('disabled');
				}
				else if($el_it_scroll.scrollTop() === 0) {
					$('.ar-l',$el_it).addClass('disabled');
					$('.ar-r',$el_it).removeClass('disabled');
				}
				else if(parseInt($el_it_scroll_hgh - 180) <= $el_it_scroll.scrollTop()) {
					$('.ar-r',$el_it).addClass('disabled');
					$('.ar-l',$el_it).removeClass('disabled');
					
				}
				else {
					$('.ar-r',$el_it).removeClass('disabled');
					$('.ar-l',$el_it).removeClass('disabled');
				}
				
			}
			
			checkarrows();
				
		});
	};
	
	$('<div id="pp-gallery-big"><div id="pp-gallery-big-i"><div id="pp-gallery-photos"><ul></ul></div></div></div> <div id="pp-gallery-thumb"><div class="ar-t"><a /></div><div id="pp-gallery-thumb-area"><ul class="thumb-items" /></div><div class="ar-b"><a /></div></div>').appendTo('#pp-gallery-fsc');

	$('<div id="imageZoom" />').appendTo('body');
	

	
	var $el = $('#pp-gallery-thumb'),
		$el_scroll = $('#pp-gallery-thumb-area'),
		$el_scroll_hgh;
		
		$el_scroll.scrollTop(0);
	
	$('.galleryseries-area a').click(function(){

		var $pp__height = $(window).height() - 80
			galseries = $(this).parents('.galleryseries-area'),
			ultra__thumb = '',
			ultra__photo = '';
		
		if($pp__height < 531) {
			$pp__height = 530
		}
		
		if($pp__height > 759) {
			$pp__height = 760
		}
		
		var $pp__width = $pp__height * ratio;
		
		if(($pp__width + 145) > $(window).width()) {
			var diff = ($pp__width + 145) - $(window).width();
			
			$pp__width = $pp__width - diff
			
		}
		else {
			var diff = 0;
		}
		
		if($pp__width > 1139) {
			$pp__height = 760;
			$pp__width = $pp__height * ratio;
		}

		$('#pp-gallery-fsc').css({
			width:($pp__width - diff) + 145 + 'px',
			marginLeft: '-'+($pp__width - diff + 145)/2 +'px'
		})
		
		$('#pp-gallery-photos').height($pp__height - diff);

		$('#overlay,#pp-gallery-fsc').show();
		
		$('body').addClass('pp-gallery-show');
		
		$('#pp-gallery-photos').scrollLeft(0);
		
		$('li',galseries).each(function(){

			var $t = $(this),
				$t__cls = $t.attr('class');
			
			if($('a',$t).data('ultraphoto')) {
				ultra__thumb += '<li data-photo-count='+ $('a',$t).data('photo-count') + '><a href='+$('a',$t).data('ultraphoto')+'><img src='+ $('img',$t).attr('src') +' /></a></li>';
				if($('a',$t).data('ultrabigphoto')) {
					ultra__photo += '<li data-photo-count='+ $('a',$t).data('photo-count') + '><a href='+$('a',$t).data('ultrabigphoto')+'><img src='+ $('a',$t).data('ultraphoto') +' /><i class="zoom"></i></a></li>';
				}
				else {
					ultra__photo += '<li data-photo-count='+ $('a',$t).data('photo-count') + '><img src='+ $('a',$t).data('ultraphoto') +' /></li>';
				}
			}
			
			$('#pp-gallery-photos ul').html(ultra__photo);
			$('#pp-gallery-thumb-area ul').html(ultra__thumb);
			
		});
		
		var dph = $(this).data('photo-count'),
			dphscroll = $('#pp-gallery-photos li[data-photo-count='+dph+']');
			
			$el_scroll_hgh = $('ul',$el_scroll).height();
		
		$('#pp-gallery-photos').scrollTo( dphscroll, 1, {
			axis:'x'
		});
		
		$el_scroll.scrollTo('li[data-photo-count='+dph+']', 1, {
			axis:'y',
			onAfter:function(){
				checkgalar();
			}
		});
		
		$('#pp-gallery-thumb-area a').removeClass('active');
		$('#pp-gallery-thumb-area li[data-photo-count='+dph+'] a').addClass('active');
		
		return false;
	});
	
	$('.gallery-ph a').click(function(){
		var par = $(this).parent().next().find('.galleryseries-area__ul a').eq(0).trigger('click');
//		$('.galleryseries-area__ul a').eq(0).trigger('click');
		return false;
	});
	
	$('#pp-gallery-thumb-area a').live('click',function(){
		
		var dph = $(this).parent().data('photo-count'),
			dphscroll = $('#pp-gallery-photos li[data-photo-count='+dph+']');

		$('#pp-gallery-thumb-area a').removeClass('active');
		$('#pp-gallery-thumb-area li[data-photo-count='+dph+'] a').addClass('active');

		$('#pp-gallery-photos').scrollTo( dphscroll, 800, {
			axis:'x'
		});
		
		return false;
	});
	
	$('#pp-gallery-photos a').live('click',function(e){
		$('#imageZoom').show();
		$('<img src='+ $(this).attr('href')+' id="imagezoom__img" />').appendTo('#imageZoom');
		
		$('#imagezoom__img').bind('load',function(){
			var div = $('#imageZoom'),
				ig = $('#imagezoom__img'),
				divWidth = div.width(),
				divHeight = div.height(),
				igW = ig.width(),
				igH = ig.height(),
				dOs = div.offset(),
				leftPan = (e.pageX - dOs.left) * (divWidth - igW) / (divWidth),
				topPan = (e.pageY - dOs.top) * (divHeight - igH) / (divHeight);
				ig.css({left: leftPan, top: topPan});
		})
		return false;
	});
	
	
	$('.ar-t a',$el).bind('click', function(){
		$el_scroll.scrollTo( '-=240px', 500, {
			axis:'y',
			onAfter:function(){
				checkgalar();
			}
		});
		return false;
	});
	
	$('.ar-b a',$el).bind('click', function(){
		$el_scroll.scrollTo( '+=240px', 500, {
			axis:'y',
			onAfter:function(){
				checkgalar();
			}
		});
		return false;
	});
	
	$('#overlay, #close').click(function(){
		$('body').removeClass('pp-gallery-show');
		$('#overlay,#pp-gallery-fsc').hide();
		return false;
	});
	
	$("#imageZoom").miniZoomPan({
	
        sW: $(window).width(),

        sH: $(window).height(),

        lW: 2400,

        lH: 1600,

        loaderContent: "<span class=loader />",

        callback: function () {
//            scaleImage();
        }

    });
    
    function scaleImage() {
    	
    };
    
    function checkgalar() {
    
    	if($el_scroll_hgh <= 480) {
    		$('.ar-t a',$el).addClass('disabled');
    		$('.ar-b a',$el).addClass('disabled');
    	}
    	else if($el_scroll.scrollTop() == 0) {
    		$('.ar-t a',$el).addClass('disabled');
    		$('.ar-b a',$el).removeClass('disabled');
    	}
    	else if(parseInt($el_scroll_hgh - 480) <= $el_scroll.scrollTop()) {
    		$('.ar-t a',$el).removeClass('disabled');
    		$('.ar-b a',$el).addClass('disabled');
    	}
    	else {
    		$('.ar-t a',$el).removeClass('disabled');
    		$('.ar-b a',$el).removeClass('disabled');
    	}
    };

	
}
