function changeSort(sort, order, changeSort) {
	var loc;
	
	if(changeSort) {
		if(order == 0) {
			order = 1;
		} else {
			order = 0;
		}
		loc = replaceInUrl('orderBy=', order);
	} else {
		loc = replaceInUrl('sortBy=', sort);
	}
	
	location = loc;
}

function changeView(type){
	var loc;
	loc = replaceInUrl('view=', type);

	location = loc;
}

function changeSort_select(select) {
	var sort = select.value;
	var order, loc;
	if(sort.length > 1) {
		var order = sort.substr(1,1);
		if(order == '-') {
			order = 0;
		} else {
			order = 1;
		}
		sort = sort.substr(0,1);
	}
	
	loc = replaceInUrl('sortBy=', sort);
	if(order != null) {
		loc = replaceInUrl('orderBy=', order, loc);
	}
	
	location = loc;
}


function replaceInUrl(what, value, loc) {
	if(!loc) {
		var loc = location.href;
	}
	var pos = loc.lastIndexOf(what);
	
	var regex = new RegExp(what+"([a-zA-Z0-9]+)?");
	
	if ( pos > -1) loc = loc.replace(regex, what + value);
	else {
		if(loc.indexOf('?') > -1) loc += '&' + what + value;
		else loc += '?' + what + value;
	}
	return loc;
}


// -- temp functions -- //
function show_big_img() {
	if($('#inv_div img').attr('showed')) {
		return ;
	}
	$.blockUI({ 
        message: $('#inv_div').html(), 
        css: { 
            top:  ($(window).height() - $('#inv_div img').attr('height')) / 2 + 'px', 
            left: ($(window).width() - $('#inv_div img').attr('width')) / 2 + 'px',
             backgroundColor: '#EEEEEE', 
        		'-webkit-border-radius': '10px', 
        		'-moz-border-radius': '10px', 
            width: $('#inv_div img').attr('width')+'px',
            padding:  '20px',
            cursor: 'default'
        }
    });

    $('.blockOverlay').attr('title','Нажмите чтобы закрыть').click($.unblockUI);
    $('#inv_div img').attr('showed', '1');
}

function resizeGallery() {

	if(jQuery(document.body).width() < 1253) {
		jQuery('#gallery_cont').width('530px');
	} else	{
		jQuery('#gallery_cont').width('700px');
	}
}

function make_gallery() {
	$('a[rel="gallery"]').click(function(i) {
		var img = $('#image img').get(0);
		img.style.width = 'auto';
		img.style.height = 'auto';
		img.src = this.href;

		var photo_size = this.getAttribute('photo_size');

		if(!photo_size){
			img.removeAttribute('photo_size');
		}
		else{
			img.setAttribute('photo_size', this.getAttribute('photo_size'));
		}
		
		$('a[rel="gallery"]').removeClass('active');
		$(this).addClass('active');
		$(this).blur();
		
		if(!$(this).attr('photo_size')) {
			$('a.zoom_img').css({cursor: 'default'});
			$('#zoom_img_lnk').hide();
		} else {
			$('a.zoom_img').css({cursor: 'hand', cursor: 'pointer'});
			$('#zoom_img_lnk').show();
		}
		
		return false;
	});
	
	
	if($('p#image').attr('photo_size')) {
		$('p#image img').attr('photo_size', $('p#image').attr('photo_size'));
	} else {
		$('a.zoom_img').get(0).disabled = true;
		$('a.zoom_img').css({cursor: 'default'});
		$('#zoom_img_lnk').hide();
	}
	
	$('a.zoom_img').click(function () {
		var src = $('p#image img').get(0).src;

		src = src.replace('/r_580_550', '/r_1024_h');

		var photo_size = $('p#image img').attr('photo_size');
		if(!photo_size) {
			return false;
		}
		var width = photo_size.split(',')[0];
		var height = photo_size.split(',')[1];
		
		window.open(src, '_blank', 'width=' + width +',height=' + height +',toolbar=0');
		
		return false;
	});
}

jQuery(document).ready(function(event){
	
	jQuery(window).resize(resizeGallery);

});
