if(!Array.prototype.indexOf){
    Array.prototype.indexOf=function(obj,start){
        for(var i=(start||0),j=this.length;i<j;i++){
            if(this[i]==obj){return i;}
        }
        return -1;
    }
}

var addthis_config = {
  data_use_cookies:false,
  data_use_flash: false
}

Cufon.replace('h2', { fontFamily: 'Victor', fontSize: '38px', lineHeight: '1.8', textTransform:'uppercase' });
Cufon.replace('h3', { fontFamily: 'Victor', fontSize: '24px', lineHeight: '1.8', textTransform:'uppercase' });


(function($) {
  $._voter = {
    addItem: function addObject(list, id) {
      if (list.indexOf(id)==-1) {
	list.push(id);
      }
    },
    //element: null,
    percent: 0,
    votes: 0,
    setupVoterElement: function() {
      //update votes text
      var textElement = $($._voter.config.textElement);
      $._voter.textElement = textElement;
      textElement.text($._voter.votes);
      
      // get element
      var element = $($._voter.config.element).eq(0);
      if (element.length>0) $._voter.element = element;
  
      //transform element height
      var height = (($._voter.percent/100) * $._voter.config.maxHeight);
      element.animate({'height':height},{'easing': 'easeOutBack','duration': 'slow'});
    }
  }
  
  $.fn.voter = function(settings) {
    var config = {'maxHeight':100, 'recipientAttribute':'votee','element':'#grade','textElement':'#votes'};
    if (settings) $.extend(config, settings);
    $._voter.config = config;
    var recipient = $(this).attr(config.recipientAttribute);
    $._voter.recipient = recipient;
    /*fetch stats - votes and percentile
      if stats in cookie - use it
      else fetch and store in cookie */
    var cookie = $.cookies.get("voter-stats");

    if (!(typeof cookie === 'object' && cookie instanceof Object)) {
      cookie = {};
    }
    
    var values = cookie[recipient];
    if(values!==undefined && values instanceof Array){
      //get values from cookie
	$._voter.votes = values[0];
	$._voter.percent = values[1]
	$._voter.setupVoterElement();
    } else {
      //fetch data
      $.post('/voter/stats/', {'recipient': recipient}, function(data) {
	if (data!==null) {
	  //alert(data);
	  $._voter.votes = data[0];
	  $._voter.percent = data[1];
	  cookie[recipient] = new Array($._voter.votes, $._voter.percent);
	  var d = new Date();
	  d.setTime(d.getTime() + (300*1000));
	  $.cookies.set("voter-stats",cookie,{'expiresAt':d});
	  $._voter.setupVoterElement();
	}
      }, 'json');
    }
    
  }
  
  $._voter.bounce = function() {
    var element = $._voter.element;
    if (element!==undefined) {
      var current=element.height();
      element.animate({'height':current+5},{'easing': 'easeOutBounce','duration': 'medium'}).animate({'height':current}, {'duration':'fast'});
    }
    
    $._voter.votes += 1;
    var cookie = $.cookies.get('voter-stats');
    if (typeof cookie === 'object' && cookie instanceof Object) {
      cookie[$._voter.recipient] = new Array($._voter.votes, $._voter.percent);
      var d = new Date();
      d.setTime(d.getTime() + (240*1000));
      $.cookies.set('voter-stats',cookie,{'expiresAt':d});
    }
   
    $._voter.textElement.html($._voter.votes); 
  }
  
  $.fn.addvote = function(settings) {
    
    var config = {'cookiebase':'voter','context':'default',"imageOff":"thumb1.png","imageOn":"thumb1-on.png","idAttribute":"id","hoursToLive":720};
    if (typeof voter_recipient=='string') {
      config["recipient"] = voter_recipient;
    }
    if (settings) $.extend(config, settings);
    var recipient = config.recipient;
    var cookiename = config.cookiebase +'-' +config.context;
    var cookies = $.cookies.get(cookiename);
    if (!(typeof cookies === 'object' && cookies instanceof Array)) {
      cookies = new Array();
    }

    $(this).each(function() {
      //make sure we have a reference id
      if ($(this).attr(config.idAttribute)=="") return this;
      //alert($(this).attr(config.idAttribute));
      if(cookies.indexOf($(this).attr(config.idAttribute))>-1){
	var src = $(this).attr("src");
	//alert(src);
	$(this).attr("src",src.replace(config.imageOff,config.imageOn)).addClass('voted');
      } else {
	$(this).css({"cursor":"pointer"}).click(function () {
	src = $(this).attr('src');
	id = $(this).attr(config.idAttribute);
	$._voter.addItem(cookies, id);
	$(this).css({"cursor":""}).attr('src',src.replace(config.imageOff,config.imageOn)).addClass('voted');
	$(this).unbind('click');
	//$._voter.bounce();
        $.post('/voter/', {id: id, c: config.context, recipient: recipient}, function(response) {
	      //var d = new Date();
	      //d.setTime(d.getTime() + (config.hoursToLive * 3600000));
	      $.cookies.set( cookiename, cookies, {'hoursToLive':config.hoursToLive});
	      if (response=='ok') {
		$._voter.bounce();
		}
            });
	});
      }
      return this;
      });
    
    return this;
  }
})(jQuery);


(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

$(document).ready(function(){
  $('img.likeicon','div#tweetcol').addvote({'context': 'twitter'}).filter('.voted').attr('title','Gillad!');
  $('div#blogcontainer img.likeicon').addvote({'context': 'blog'}).filter('.voted').attr('title','Gillad!');
  $('img#bigthumb').addvote({"imageOff":"thumb_big.png","imageOn":"thumb_big.png","idAttribute":"voteid"});
  $('div#therm').voter({'maxHeight':115,'recipientAttribute':'voterurl','element':'img#colour','textElement':'span#votes'});
  //$('img.voted').attr('alt','Gillad!');
  //"Popups"
  $(".film").colorbox({iframe:true, innerWidth:640, innerHeight:400, close:'Stäng'});
  $(".paper").colorbox({iframe:true, innerWidth:800, innerHeight:566, close:'Stäng'});
  $(".criterias").colorbox({iframe:true, innerWidth:640, innerHeight:570, close:'Stäng'});
  $("a[rel=lightbox]").colorbox({'previous':'','next':'','current':'',close:'Stäng'});
  $("a#emailform").colorbox({iframe:true, innerWidth:500, innerHeight:250, close:'Stäng'});
  //Rollovers
  $(".rollover").hover(
    function(){
	    if($(this).attr("src").indexOf("-on") == -1) {
		    var newSrc = $(this).attr("src").replace(".png","-on.png#hover");
		    $(this).attr("src",newSrc);
	    }
    },
    function(){
	    if($(this).attr("src").indexOf("-on.png#hover") != -1) {
		    var oldSrc = $(this).attr("src").replace("-on.png#hover",".png");
		    $(this).attr("src",oldSrc);
	    }
    }
  );
  
  $("a[rel=external]").attr('target','_blank');
  
  //Form fields text input
  $('input[type="text"]').addClass("idleField");
  $('input[type="text"].initial').focus(function() {
      $(this).removeClass("idleField").addClass("focusField");
      if (this.value == this.defaultValue){ this.value = ''; }
      if(this.value != this.defaultValue){ this.select(); }
  });
  $('input[type="text"].initial').blur(function() {
      $(this).removeClass("focusField").addClass("idleField");
      if ($.trim(this.value) == ''){ this.value = (this.defaultValue ? this.defaultValue : ''); }
  });
  
  $(document).ready(function(){ $("#mainmenu li").hover( function() { $(this).addClass("iehover"); }, function() { $(this).removeClass("iehover"); } ); });
  //Form fields textareas
  //$('textarea').addClass("idleField");
  //$('textarea').focus(function() { $(this).removeClass("idleField").addClass("focusField");  });
  //$('textarea').blur(function() { $(this).removeClass("focusField").addClass("idleField"); });
});		


$(document).ready(function(){
  $.preLoadImages('/media/images/menu/on.png','/media/images/menu/on2.png','/media/images/logos/l1-on.png','/media/images/logos/l2-on.png','/media/images/logos/l3-on.png','/media/images/logos/l4-on.png','/media/images/logos/l5-on.png','/media/images/logos/l6-on.png');
});