function magnify(title, guid){
	var data = document.viewport.getDimensions();
    var width = data['width'];
    var height = data['height']-70;
    var added_query = "&b_width=" + width + "&b_height=" + height;
	GB_showImage(title, get_photo(guid, 'o', added_query));
}

var photo_tags = [];

function updatePhotoInformation(transport){

    var response=transport.responseText;
    var jsonObj=eval("("+response+")");

    var average_rating = jsonObj.average_rating;
    var user_score = parseInt(jsonObj.user_score);
    var viewed = jsonObj.viewed;
    var html_comment = jsonObj.html_comment;
    var title = jsonObj.title;
    var desc = jsonObj.desc;
    var tags = jsonObj.tags;
    var photographer_link = jsonObj.link;
    var photographer = jsonObj.photographer;
    
    var exif = jsonObj.exif;
    photo_tags = jsonObj.photo_tags;
    
    SetWidthHeight(parseInt(jsonObj.owidth),parseInt(jsonObj.oheight));

    $('photographer').href=photographer_link
    $('photographer').innerHTML=photographer;
    $('score').innerHTML = average_rating;

    if(anonymous==0)
    	updateUIStars(user_score);

    $('viewed').innerHTML = viewed;
	$('tags').innerHTML = tags;

    $('comments').innerHTML = html_comment;
    $('title').innerHTML = title;
    $('description').innerHTML = desc;

    $('exif').innerHTML = exif;
    
    callback_updatePhotoInformation();
}
	
function getPhotoInformationInternal(photo_guid){
	
	if(debug){
		target_url = "/api/photo/information/";
	}else{
		target_url = location.protocol + '//' + location.host + "/api/photo/information/";
	}

    new Ajax.Request(target_url,		
        {
            method: "POST",
            parameters: "theme=" + theme + "&photo_guid="+ photo_guid + "&album_id="+ collection_id +"&next="+escape(current_url),
            onSuccess: updatePhotoInformation
        }
    );
}

function get_images_for_cache(index, amount, direction){

    var album_info = "&album=" + escape(collection_id);
    
    if(typeof(filter) == 'undefined'){
        var filter_info = '';
    }else{
        var filter_info = "&filter=" + filter;
    }
    // Call the server...
    new Ajax.Request(
    		location.protocol + '//' + location.host + "/api/get_photos_in_collection/",
        {
            method: "POST",
            parameters: "amount="+ amount +"&direction="+direction+"&index="+index+ album_info + filter_info,
            onSuccess: update_get_images_for_cache
        }
    );
}

function getPhotoInformation(photo_guid){
	getPhotoInformationInternal(photo_guid);
}

function updatePreviews(transport){
    var response=transport.responseText;
    var jsonObj=eval("("+response+")");
    
    var images = jsonObj.images;
    var position = jsonObj.positions;
    var index = jsonObj.indexes;
    var direction = jsonObj.direction;
    var widths = jsonObj.widths;
    var heights = jsonObj.heights;
    var target_index = jsonObj.target_index;
    size_of_collection = jsonObj.collection_size;

    image_index = target_index;

    index_in_collection = index[0];
    
    var p_ratio = 0;
    var first = false;
    for(x=0;x<images.length;x++){
        if(x==0)
        	page_index = index[x];
        	//temp_ratio = heights[x][1]/widths[x][1];
    	if(main_photo_guid=='' || images[x]==main_photo_guid){
    	    if(main_photo_guid==''){
    	       main_photo_guid = images[x];
    	       first = true;
    	    }
    		index_in_collection = index[x];
    		p_ratio = parseInt(heights[x][1])/parseInt(widths[x][1]);
    		//alert(heights[x][1]);
    		//alert(widths[x][1]);
    		//alert(p_ratio);
    	} 
    	
    	image_src = callback_get_image_src(images[x]);
    	Preloader.add(images[x], image_src, index[x],{'width':widths[x], 'height':heights[x]});
    	if(index[x]<lower_cache_index)
        	lower_cache_index = index[x];
    	if(index[x]>upper_cache_index)
    		upper_cache_index = index[x];
    } 
 
    data = Preloader.loadImage('image', index_in_collection);

    callback_set_main_photo(data['image'], p_ratio);
    main_photo_guid = data['image'];
    window.location.hash = "photo=" + main_photo_guid;

    getPhotoInformationInternal(main_photo_guid);

    callback_preview_init_complete(data['image'], data['index']);
    
    
    
    
    // Get the next batch of photos
    if(lower_cache_index>page_index-9){
    	get_images_for_cache(lower_cache_index, 9, 0);
    }
    if(upper_cache_index<page_index+18){
    	get_images_for_cache(upper_cache_index, 9, 1);
    }
    
    AnchorChecker.initialize();
    if(first){
        //log('after');
        Event.observe('main_photo', 'load', main_photo_loaded.bind(this,data['image'], p_ratio, data['index']));
    }
}
function main_photo_loaded(guid, ratio, index, event){
    
    Event.stopObserving('main_photo', 'load');
    callback_set_main_photo(guid, ratio);
    
}

function SetupPreviews(photo_guid, amount){
    var album_info = "&album=" + escape(collection_id);
    
    if(typeof(filter) == 'undefined'){
        var filter_info = '';
    }else{
        var filter_info = "&filter=" + filter;
    }
    
    // Call the server...
    new Ajax.Request(
    		location.protocol + '//' + location.host + "/api/get_photos_in_collection/",
        {
            method: "POST",
            parameters: "mod=9&amount="+ amount +"&direction=2&photo_guid="+photo_guid+ album_info + filter_info,
            onSuccess: updatePreviews
        }
    );

}

function update_get_images_for_cache(transport)
{
    var response=transport.responseText;
    var jsonObj=eval("("+response+")");
    
    var images = jsonObj.images;
    var position = jsonObj.positions;
    var index = jsonObj.indexes;
    var direction = jsonObj.direction;
    var widths = jsonObj.widths;
    var heights = jsonObj.heights;
   
    for(x=0;x<images.length;x++){
    	//alert("blah:" + x + "   image:" + images[x]);
        Preloader.add(images[x], get_photo(images[x], 'm'), index[x],{'width':widths[x], 'height':heights[x]});
        if(index[x]<lower_cache_index)
        	lower_cache_index = index[x];
    	if(index[x]>upper_cache_index)
    		upper_cache_index = index[x];
    }
 
}

function readHashVars()
{
    var hash = window.location.hash;
    hash = hash.substring(1);
    hash = hash.replace(/\|/g, '&');
    hash = hash.replace(/\:/g, '=');
    // lets turn our url hash into a javascript hash (like an associated array)
    // we will use a useful function provided in the prototype library
    hash = hash.parseQuery();
    return hash;
}

var AnchorChecker = {
	initialize: function(){
	this.location = location.href;
	this.interval = setInterval(function(){
		if (this.location != location.href)
		{
			this.anchorAltered();
			this.location = location.href;
		}}.bind(this), 500); // check every half second
	},
	anchorAltered: function(){
		   // the url has been altered
		hash_vars = readHashVars();
    	if(hash_vars['photo']!=main_photo_guid){
    		data = Preloader.loadImageByValue('image', hash_vars['photo']);
    		preview_click(data['index']);
    	}
	}
};

function updateRating(transport){
    var response=transport.responseText;
    var jsonObj=eval("("+response+")");
    $('score').innerHTML = jsonObj.score
};

function setRating(rating){
	if(anonymous!=1){
	    new Ajax.Request(
	    		location.protocol + '//' + location.host + "/photo/"+ main_photo_guid +"/rate/" + rating + "/",
	        {
	            method: "POST",
	            onSuccess: updateRating
	        }
	    );
	    //popup_timeout('Photo Rating', 'You have rated this photo <b>' + rating + '</b> stars', popup_time_quick);
	    updateUIStars(rating);
	}
};

function updateShareSubmit(transport)
{
    var response=transport.responseText;
    var jsonObj=eval("("+response+")");
    
    if(jsonObj.success){
    	closeShareSubmit();
    	show_msg("Email was set");
    }else{
	    $('contacts').value = jsonObj.bad;
	    $('error_msg').setStyle({'color':'#DA7B5B'});
	    $('error_msg').innerHTML = jsonObj.message;
    }
	
}

function closeShareSubmit()
{
    $('contacts').value = '';
    $('sender').value = '';
    contentWin.close();
}

function ShareSubmit(album_id, photos){
    var contacts = $F('contacts');
    var sender = $F('sender');
    //var str = document.location;
    var str = String(document.location).replace('#photo','?photo');
    var page = escape(str);
    
    var album_info = album_id;
    new Ajax.Request(
    		location.protocol + '//' + location.host + "/photo/share/",
        {
            method: "POST",
            parameters: "sender=" + escape(sender) + "&page=" + page +"&contacts=" + escape(contacts) + "&photo=" + photos + "&album=" + album_info,
            onSuccess: updateShareSubmit
        }
    );
}

function prepare_login_link(){
    var str = String(document.location); //.replace('#photo','?photo');
    var index = str.indexOf('#photo');
    if(index>-1){
        var hash_part = str.substr(index);
        return hash_part;
    }
    return '';

}