jQuery.noConflict();
(function($) { 
  $(function() {
//BEGIN SS-JS
$(document).ready(function(){
	galleryFunct();
	getMainItem();
	$('.listings_group img').click(function(){
		$('.main_gallery_item').slideDown(1000);
	});
});
function galleryFunct(){
	setTimeout(function(){fixMargins();},200);
	$('.gallery_image img').eq(0).addClass('active_gallery_image').fadeIn();
	
	arrowFix();
	$('.arrow').live('click',function(){
		var type = $(this).hasClass('arrow_left') == true?'left':'right';
		var posnow = $('.gallery_image img').index($('.active_gallery_image'));
		var len = $('.gallery_image img').length;
		if(type=='right'){
			posnow < (len-1)?advanceMain(posnow):resetMain(posnow);
		}
		else{
			posnow > 0?minusMain(posnow):resetMain(posnow);			
		}
		
		return false;
	});
}
function fixMargins(){
	$.each($('.listings_group li'),function(i,x){
		var ih = $('img',this).height();
		var iw = $('img',this).width();
		if(ih > iw){
			var extr = Math.floor((200/ih)*iw);
			var corrwidth = (200-extr)/2;
		$('img',this).height(200).width(extr);
		
		}
		var corrheight = (200-ih)/2;
		
		if(ih > iw){
			//console.log(corrwidth);
			$('img',this).css('margin-left',corrwidth+'px');
		}
		else{
		$('img',this).css('margin-top',corrheight+'px');
		}
	});
}
function arrowFix(){
	var galheight = $('.active_gallery_image').height();
	var arrowheight = $('.arrow arrow_left').height();
	var fixed = (galheight-100)/2;
	//console.log(galheight+' '+arrowheight+' '+fixed);
	$('.arrow').css('margin-top',fixed+'px');
}
function advanceMain(pos){
 var newpos = pos+1;	
 //console.log(newpos);
 $('.gallery_image img').removeClass('active_gallery_image').hide();
 $('.gallery_image img').eq(newpos).addClass('active_gallery_image').fadeIn();
}
function minusMain(pos){
	 var newpos = pos-1;
	 $('.gallery_image img').removeClass('active_gallery_image').hide();
	 $('.gallery_image img').eq(newpos).addClass('active_gallery_image').fadeIn();
	}
function resetMain(pos){
 var newpos = pos == 0?($('.gallery_image img').length-1):0;
 $('.gallery_image img').removeClass('active_gallery_image').hide();
 $('.gallery_image img').eq(newpos).addClass('active_gallery_image').fadeIn();
}
function checkMainMargin(){
	$('.gallery_image img').each(function(i,x){
		var ih = $(this).height();
		var iw = $(this).width();
		if(ih > iw){
			var extr = Math.floor((415/ih)*iw);
			var corrwidth = (415-extr)/2;
		$(this).css('height','415px').css('width',extr+'px');
		
		}
		var corrheight = (415-ih)/2;
		
		if(ih > iw){
			//console.log(corrwidth);
			$(this).css('margin-left',corrwidth+'px');
		}
		else{
		//$(this).css('margin-top',corrheight+'px');
		}
	});
	
}
function getMainItem(){
$('.main_gallery_thumbs img, .listings_group img').live('click',function(){
	$('.main_gallery_item').html('<img style="margin: 25% 50%;" src="/wp-content/themes/portfolio-press/images/ajax-loader.gif" alt="loading" />');
	var id = $(this).attr('rel');
	$.post('/wp-content/themes/portfolio-press/ss_main_item.php','id='+id,function(data){
		
		$('.main_gallery_item').html(data);
		galleryFunct();
		checkMainMargin();
	});
});
}

//END SS-JS
  });
})(jQuery);

