
(function($) {
    
    $.fn.filestyle = function(options) {
                
        /* TODO: This should not override CSS. */
        var settings = {
            width : 250
        };
                
        if(options) {
            $.extend(settings, options);
        };
                        
        return this.each(function() {
            
            var self = this;
            var wrapper = $("<div>")
                            .css({
                                "width": settings.imagewidth + "px",
                                "height": settings.imageheight + "px",
                                "background": "url(" + settings.image + ") 0 0 no-repeat",
                                "background-position": "right",
                                "display": "inline",
                                "position": "absolute",
                                "overflow": "hidden"
                            });
                            
            var filename = $('<input class="file">')
                             .addClass($(self).attr("class"))
                             .css({
                                 "display": "inline",
                                 "width": settings.width + "px"
                             });

            $(self).before(filename);
            $(self).wrap(wrapper);

            $(self).css({
                        "position": "relative",
                        "height": settings.imageheight + "px",
                        "width": settings.width + "px",
                        "display": "inline",
                        "cursor": "pointer",
                        "opacity": "0.0"
                    });

            if ($.browser.mozilla) {
                if (/Win/.test(navigator.platform)) {
                    $(self).css("margin-left", "-142px");                    
                } else {
                    $(self).css("margin-left", "-168px");                    
                };
            } else {
                $(self).css("margin-left", settings.imagewidth - settings.width + "px");                
            };

            $(self).bind("change", function() {
                filename.val($(self).val());
            });
      
        });
        

    };
    
})(jQuery);

$.fn.toEm = function(settings){
 settings = jQuery.extend({
  scope: 'body'
 }, settings);
 var that = parseInt(this[0],10);
 var scopeTest = jQuery('<div style="display: none; font-size: 1em; margin: 0; padding:0; height: auto; line-height: 1; border:0;"> </div>').appendTo(settings.scope);
 var scopeVal = scopeTest.height();
 scopeTest.remove();
 return (that / scopeVal).toFixed(8) + 'em';
};


$.fn.toPx = function(settings){
 settings = jQuery.extend({
  scope: 'body'
 }, settings);
 var that = parseFloat(this[0]);
 var scopeTest = jQuery('<div style="display: none; font-size: 1em; margin: 0; padding:0; height: auto; line-height: 1; border:0;"> </div>').appendTo(settings.scope);
 var scopeVal = scopeTest.height();
 scopeTest.remove();
 return Math.round(that * scopeVal) + 'px';
};

$.fn.equalHeights = function(px) {
 $(this).each(function(){
  var currentTallest = 0;
  $(this).children().each(function(i){
   if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
  });
  //if (!px || !Number.prototype.toEm) currentTallest = currentTallest.toEm(); //use ems unless px is specified
  // for ie6, set height since min-height isn't supported
  if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
  $(this).children().css({'min-height': currentTallest}); 
 });
 return this;
};


var justvoted = false;
function vote(id) {
  $.ajax({
   url: '/page/38?vote=' + id,
   success: function(data) {
    $.cookie("ppa-vote", "true", { expires: 1 });
    $("#voteok").show("fast");
    justvoted = true;
   }
  });
}



$(document).ready(function() { 
 // Menu 
 $('#nav').superfish({
  
 });

  
 $('.card img').click(function () {
  $(".card img").each(function(){
   $(this).removeClass('sel');
  });
  
  $(this).addClass('sel');
  $('input[name=theme]').val($(this).attr('id'));
 
    });
 
 
 $('.upload_btn').fancybox({
  'width'    : 690,
  'height'   : 540,
        'autoScale'      : false,
  'scrolling'   : 'auto',
  'type'    : 'iframe',
  'hideOnOverlayClick': false,
  'overlayOpacity' : 0.2,
  'onClosed': function() {
   parent.location.reload(true);
  } 
 });

 $('.votelink-popup').fancybox({
        'autoScale'      : true,
  'hideOnOverlayClick': false,
  'type'    : 'ajax',
  'overlayOpacity' : 0.2,
  'onClosed': function() {
   if (justvoted) {
    justvoted = false;
    parent.location.reload(true);
   } 
  } 
 });
 
 $('#submitcar').click(function () { 
  var name = $('[name=name]').val();
  var address = $('[name=address]').val();
  var zip = $('[name=zip]').val();
  var city = $('input[name=city]').val();
  var email = $('[name=email]').val();
  var phone = $('[name=phone]').val();
  
  var imagefile = $('[name=imagefile]').val();
  
  
  if(name!='' && phone!='' && address!='' && zip!='' && city!='' && imagefile!=''){
   var datastring = 'name='+name+'&phone='+phone+'&address='+address+'&zip='+zip+'&city='+city+'&email='+email;
   $('#uploadform').submit();
  } else {
  
   if (name!='' && phone!='' && address!='' && zip!='' && city!='' && imagefile=='') {
    $("#submitfile").show("fast");
   } else {
   
   if(name==''){
    $('#ff_name').css('background', 'url(/resources/public/piparkakkuskaba/kentta_2.png) no-repeat');
   }
   if(phone==''){
    $('#ff_phone').css('background', 'url(/resources/public/piparkakkuskaba/kentta_2.png) no-repeat');
   }
   if(address==''){
    $('#ff_address').css('background', 'url(/resources/public/piparkakkuskaba/kentta_2.png) no-repeat');
   }
   if(zip==''){
    $('#ff_zip').css('background', 'url(/resources/public/piparkakkuskaba/kentta_2.png) no-repeat');
   }
   if(city==''){
    $('#ff_city').css('background', 'url(/resources/public/piparkakkuskaba/kentta_2.png) no-repeat');
   }
   $("#submitfalse").show("fast");
   
   }
   
   
  }
 });
 
 
 $('#submitok').click(function () {
  $("#submitok").hide("fast");
  parent.$.fancybox.close();
 });
 
 $('#submitfalse').click(function () {
  $("#submitfalse").hide("fast");
 });
 
 $('#submitfile').click(function () {
  $("#submitfile").hide("fast");
 });
 
 $('#voteok').click(function () {
  $("#voteok").hide("fast");
 });
 
 $('#votefalse').click(function () {
  $("#votefalse").hide("fast");
 });

 $('.carcontainer').equalHeights();


}); 






