// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//TODO load dependencies: browserdetect

// Copyright(c) 2007 - Frost Innovation AS, http://ajaxwidgets.com
//
// EffectResize.js is freely distributable under the terms of an MIT-style license.
// For details, see the script.aculo.us web site: http://script.aculo.us/

/* Helper Effect for resizing elements...
 */
Effect.ReSize = Class.create();
Object.extend(Object.extend(Effect.ReSize.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = element;
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({ amount: 100, direction: 'vert', toSize:null }, arguments[1] || {});
    if( options.direction == 'vert' )
      this.originalSize = options.originalSize || parseInt(this.element.style.height);
    else
      this.originalSize = options.originalSize || parseInt(this.element.style.width);

    if( options.toSize != null )
      options.amount = options.toSize - this.originalSize;

    this.start(options);
  },
  setup: function() {
    // Prevent executing on elements not in the layout flow
    if(this.element.getStyle('display')=='none') { this.cancel(); return; }
  },
  update: function(position) {
    if( this.options.direction == 'vert' ){
      this.element.setStyle({height: this.originalSize+(this.options.amount*position)+'px'});
    } else {
      this.element.setStyle({width: this.originalSize+(this.options.amount*position)+'px'});
    }
  },
  finish: function(){
    if( this.options.direction == 'vert' ){
      this.element.setStyle({height: this.originalSize+this.options.amount+'px'});
    } else {
      this.element.setStyle({width: this.originalSize+this.options.amount+'px'});
    }
  }
});

function scrollToDiv(id) {
	if(Prototype.Browser.IE6)
		new Element.scrollToWithOffset(id, -24);
	else
		new Effect.ScrollTo(id, {offset: -24});
}


function loadExternalProfile(username){
	flickr_url = "/profile/"+username+"/get_flickr";
	var flickrAjax = new Ajax.Updater('profile_flickr',flickr_url,{method: 'post'})
	
	lastfm_url = "/profile/"+username+"/get_lastfm";
	var lastfmAjax = new Ajax.Updater('profile_lastfm',lastfm_url,{method: 'post'})
	
	blog_url = "/profile/"+username+"/get_blog";
	var blogAjax = new Ajax.Updater('profile_blog',blog_url,{method: 'post'})
}

var currentid;
function buy_photo(id){
	if(id!=currentid){
		Position.prepare();
		coords = Position.cumulativeOffset($('photo_'+id));
		$('overlay').style.left=coords[0]+166+'px';
		$('overlay').style.top=coords[1]+37+'px';
		$('overlay').innerHTML='<div id="quickbuy_loading"></div>';
		Element.show('overlay');
		var ajax = new Ajax.Updater('overlay',"/shop/quickbuy/"+id,{method: 'get'});	
		currentid=id;
	}else{
		Element.hide('overlay');
		currentid='';
	}
}

function add_photo_to_cart(size1, size2, size3, size4, id) {
	var ajax = new Ajax.Request("/shop/add_to_cart_quick",{method: 'post', asynchronous:true, evalScripts:true, parameters: "quantity=1&size[20x30]=" + size1 + "&size[30x45]=" + size2 + "&size[50x70]=" + size3 + "&size[70x100]=" + size4 + "&photo_id=" + id + "&quickbuy=true"});
}

var currentid;
function edit_cart_item(id){
	if(id!=currentid){
		Position.prepare();
		coords = Position.cumulativeOffset($('photo_'+id));
		$('overlay').style.left=coords[0]+2+'px';
		$('overlay').style.top=coords[1]+10+'px';
		$('overlay').innerHTML='<div id="quickbuy_loading"></div>';
		Element.show('overlay');
		var ajax = new Ajax.Request("/shop/edit_cart_item/"+id,{method: 'get', asynchronous:true, evalScripts:true});	
		currentid=id;
	}else{
		Element.hide('overlay');
		currentid='';
	}
}

function keyPressPhoto(e, prev, next) {
	var key = e.keyCode;
	
	switch(key) {
		case 39:
			window.location.href = next; break;
 		case 37:
			window.location.href = prev; break;

		
		
	}
}

var days_in_month=[31,28,31,30,31,30,31,31,30,31,30,31];
var short_month_names=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];

var current_date=new Date();
var current_year=parseInt(current_date.getFullYear());
var current_month=parseInt(current_date.getMonth());
var current_day=parseInt(current_date.getDate());
	
DateSlider = Class.create();
Object.extend(Object.extend(DateSlider.prototype, Abstract.prototype), {
	initialize: function(el){
		this.parent=$(el);
		this.slidervalues = [0,393];
		this.points=[
			{left:0, date: getDate(2003,0,1)},
			{left:45, date: getDate(current_year-3,0,1)},
			{left:82, date: getDate(current_year-2,0,1)},
			{left:118, date: getDate(current_year-1,0,1)},
			{left:155, date: getDate(current_year,0,1)},
			{left:183, date: getDate(current_year,Math.abs((current_month-1)/2),Math.abs( (current_day-1) /2 ) )},
			{left:213, date: getDate(current_year,current_month-1,current_day)},
			{left:247, date: getDate(current_year,(current_month-5),current_day)},
			{left:279, date: getDate(current_year,current_month,current_day-7)},
			{left:299, date: getDate(current_year,current_month,current_day-5)},
			{left:319, date: getDate(current_year,current_month,current_day-4)},
			{left:337, date: getDate(current_year,current_month,current_day-3)},
			{left:356, date: getDate(current_year,current_month,current_day-2)},
			{left:375, date: getDate(current_year,current_month,current_day-1)},
			{left:393, date: current_date}
		];
		this.steps=[0,45,82,118,155,183,213,247,279,299,319,337,356,375,393]
		
//		this.current_start_pos=this.getStartPoint();
//		this.current_end_pos=this.getEndPoint()||21;
		
		var self=this;
		this.sliderhandles = ['slider_start', 'slider_end'];
		this.slider = new Control.Slider(this.sliderhandles, 'slider', {
			range:$R(0, 393),
			values:this.steps,
			restricted:true,
			sliderValue: this.slidervalues
		});
		this.slider.options.onChange = function(offsets) {
			self.getStartPosition(offsets[0]);
			self.getEndPosition(offsets[1]);
 			self.moveHighlight(offsets);
			self.checkDistance(offsets);
		}
		
		this.slider.options.onSlide = function(offsets) {
			this.onChange(offsets)
		}
		var d = new Date();
	},
	getFirstValues: function(initindex) {
		this.slider.setValue(this.points[initindex].left)
		this.getStartPosition(this.points[initindex].left);
		this.getEndPosition(393);
	},
	moveHighlight: function(offsets) {
		$("slider_select").style.left = (offsets[0]+10) + "px";
		$("slider_select").style.width = (offsets[1]-offsets[0]) +"px";
	},		
	checkDistance: function(offsets){
		var index = this.getDateIndex(offsets[0]);
		var index2 = this.getDateIndex(offsets[1]);
		$("slider_start_label").style.left = offsets[0]-(index+1) +"px";
		$("slider_end_label").style.left = (offsets[1]-83+(index2+1)) +"px";
		if ((offsets[1]-offsets[0])<=2) {
			$("slider_end").style.left = this.points[index+1].left;
		}
		if ((offsets[1]-offsets[0])<=80) {
			$("slider_end_label").addClassName("close");
			$("slider_start_label").addClassName("close");
		}else if((offsets[1]-offsets[0])<=135) {
			$("slider_end_label").addClassName("close");
		} else {
			$("slider_end_label").removeClassName("close");
			$("slider_start_label").removeClassName("close");
		}
	},
	getDateIndex: function(offset){
		for(var i=0;i<this.points.length;i++){
			if(this.points[i].left==offset)
			return i;
		}
		return null;
	},
	getStartPosition: function(offset) {
		var date = this.points[this.getDateIndex(offset)].date;
		$("start").value = this.getFormattedDate(date);
		$("slider_start_label").innerHTML = this.getHumanFormattedDate(date);	
//		$("slider_start_label").innerHTML = this.points[0].date;
	},
	getEndPosition: function(offset) {
		var date = this.points[this.getDateIndex(offset)].date;
		$("end").value = this.getFormattedDate(date);
		$("slider_end_label").innerHTML = this.getHumanFormattedDate(date);
	},
	getFormattedDate: function(date){
		var month=parseInt(date.getMonth())+1;
		var day=parseInt(date.getDate());
		if(month<10)
			month="0"+month;
		if(day<10)
			day="0"+day;
		return""+date.getFullYear()+month+day;
	},
	getHumanFormattedDate: function(date){
		var day=parseInt(date.getDate());
		if(day<10)
			day="0"+day;
		return day+" "+short_month_names[date.getMonth()]+" "+date.getFullYear();
	}
});

function getDate(year,month,day){
	if(day<0){
		month--;
		day=days_in_month[month<0?12-Math.abs(month):month]-Math.abs(day);
	}
	if(month<0){
		month=12-Math.abs(month);year--;
	}
	return new Date(year,month,day);
}


function scrolling_list_tick(prefix, id, link) {
	var o = $(prefix + "_" + id);
	if (link)
		o.checked = !o.checked;
	var tick = $("tick_" + prefix + "_" + id);
	if (o.checked)
		tick.addClassName("checked");
	else
		tick.removeClassName("checked");
}
function scrolling_list_toggle(prefix, link) {
	var master = $(prefix + "_check_all");
	var boxes = $$("." + prefix + "_check");
	if (link)
		master.checked = !master.checked;
	
	var tick = $("tick_" + prefix + "_all");
	if (master.checked) {
	 	var check = true;
    	tick.addClassName("checked");	
	} else {
     	tick.removeClassName("checked");
	    var check = false;
	}
	for(var i=0,l=boxes.length; i<l; i++) {
		if(boxes[i].type == 'checkbox') {
			var o = $(prefix + "_" + boxes[i].value);
			o.checked = check;
			var subtick = $("tick_" + prefix + "_" + boxes[i].value)
			if (o.checked)
				subtick.addClassName("checked");
			else
				subtick.removeClassName("checked");
		}
	}
}
function scrolling_list_serialize(prefix) {
	var boxes = $$("."+ prefix + "_check");
	var output = ""
	for(var i=0,l=boxes.length; i<l; i++) {
		if(boxes[i].type == 'checkbox') {
			if (boxes[i].checked == true) {
				output += boxes[i].value + ","
			}
		}
	}
	return output;
}