// JavaScript Document

function fadein(id,speed){
$(id).fadeIn(speed);
}


//Resize image start

function initializeGrid(way_to_show) {
  $("div#container img").each(function() {

    //var width = $(this).width();
    //var height = $(this).height();
	
	if(way_to_show=='all_cover'){
	
	newwidth_k1=awidth;
	newheight_k1=(awidth*imgHeight)/imgWidth;
	
	diff_k1=aheight-newheight_k1;
	
	newheight_k2=aheight;
	newwidth_k2=(aheight*imgWidth)/imgHeight;
	
	diff_k2=awidth-newwidth_k2;
	
	if(diff_k2<0){
	newheight=newheight_k2;
	newwidth=newwidth_k2;
	
	diffHeight=0;
	diffWidth=Math.abs(diff_k2);
	} else {
	newheight=newheight_k1;
	newwidth=newwidth_k1;
	
	diffHeight=Math.abs(diff_k1);
	diffWidth=0;
	}
	
	
	}
	
	if(way_to_show=='all_in'){
	
	diffWidth=awidth-imgWidth;
	diffHeight=aheight-imgHeight;
	
	if(diffHeight<diffWidth){
	
	newheight=aheight;
	newwidth=(newheight*imgWidth)/imgHeight;

	} else {

	
	newwidth=awidth;
	newheight=(newwidth*imgHeight)/imgWidth;

	}
	}
	
	$(this).css("width",newwidth);
    $(this).css("height",newheight);

  });
  
  }
  
  
  function slide_toggle(id){
 $(id).slideToggle("slow");
}

//change theme function start
function change_theme(img_file){

$('#image_container').append($('<img/>')).html('');

$("#loading").ajaxStart(function(){
$(this).show();
});

var img = new Image();
$(img).load(
function () {

$(this).hide();
$('#image_container').removeClass('loading').append(this);
initializeGrid();
$(this).fadeIn();
}).error(function () {
// notify the user that the image could not be loaded
}).attr('src', 'images/design/'+img_file);
}


function image_query(where_to_show,img_path, img_file){

var img = new Image();
img.src = img_path+''+img_file;

$(img).hide();
$(where_to_show).html('').append(img);

return img;

}

//ajax query function start
function ajax_query(where_to_show, what_to_show, loading, has_scroller){
$('#'+where_to_show).html($('#'+loading).html());

$.ajax({
url: what_to_show,
success: function(response) {
// update status element
$('#'+loading).hide();
$('#'+where_to_show).html(response);
if(has_scroller=='true'){
$('#'+where_to_show).jScrollPane();
}

},

timeout: 9000,
error: function(){
alert('Error loading XML document');
},


error: function(xhr) {
alert('Error!  Status = ' + xhr.status);
}

});


}

//ajax query function end


function scroll_to(id, img_id,img_path, img_file){
$(id).scrollTo($(img_id), 800);
$(img_id).html('loading');
image_query(img_id,img_path, img_file);
fadein(img_id+' img',1000);
}
