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 = location.href ;

	loc = replaceInUrl('view=', type);

	location = loc;
}


function replaceInUrl(what, value) {
	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 make_gallery() {
	$('a[rel="gallery"]').click(function(i) {
		var img = $('#image img').get(0);
		img.style.height = 'auto';
		img.src = this.href;

		var photo_size = this.getAttribute('photo_size');
		
		var preview_width = this.getAttribute('preview_width');
		img.style.width = preview_width;

		if(!photo_size){
			img.removeAttribute('photo_size');
		}
		else{
			img.setAttribute('photo_size', this.getAttribute('photo_size'));
		}

		$(img).attr('width', $(this).attr('img_width'));
		$(img).attr('height', $(this).attr('img_height'));
        $('#image a').attr('href', $(this).attr('href'));
		$('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_auto', '/r_1024_auto');
		*/
        var src = $('p#image a').attr('href');
		var photo_size = $('p#image img').attr('photo_size');
		if(!photo_size || !src) {
			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;
	});
}
