iq = {};

iq.min = 0;
iq.sec = 0;
iq.save_safe = true;
iq.new_width = 749;
iq.goTo = function(){
	jQuery(".goTo").bind("click",function(){
		var ref = jQuery(this);
		var data = ref.metadata();
		var target = data.target;
		
		document.location.replace(target+'/'+iq.min+'/'+iq.sec);
	});
};

iq.count = function(){
	var ref = jQuery(".count");
	if(ref.size()){
	var data = ref.metadata();
	iq.min = data.min;
	iq.sec = data.sec;
	count_interval = setInterval(function(){ 
		if(iq.sec == 59){ 
			iq.min++; iq.sec = 00;
		 } else {
			iq.sec++;
		}
		if(iq.sec<10){
			ref.html(iq.min + ":0" + iq.sec);	
		} else {
			ref.html(iq.min + ":" + iq.sec);
		}
		iq.time_bar_stuff();
	},1000);
	}
};

iq.login_box_stuff = function() {
  
  jQuery(".show_login_box").bind('click',
      function(e) {
        e.preventDefault();
        UserPanel = jQuery('.UserPanel');
      if((UserPanel.hasClass('hide')) || (UserPanel.hasClass('done')))
      {
      UserPanel.show('normal');
      UserPanel.removeClass('hide');
      UserPanel.removeClass('done');
      }
      else {
      UserPanel.hide('normal');
      UserPanel.addClass('done');
    }
    }
    );
  
}

iq.animate_score = function () {
  
  piramid = jQuery('.piramid');
  var new_width = piramid.attr('rel');
  new_width = new_width+'px';
  
  piramid.animate({width: new_width}, 6000);
  
}
iq.paypal_form_stuff = function() {
  paypal_form =   jQuery('.codebox_paypal');
  paypal_form.find('em').click(function(){  
    paypal_form.css('height', 'auto');
    paypal_form.find('form').show('normal');
    jQuery(this).hide();
  }
  );
  
}

iq.country_change_form_stuff = function(){
  jQuery('.country_form span em').click(
    function(){
      jQuery(this).parent().hide();
      jQuery('.country_form form').show('fast');
    }
  );
  
  
  country_ch = jQuery('#change_country');
  country_ch.change(
    function() {
      alias = '/' + jQuery(this).val();
      jQuery.post(alias, false, setTimeout("window.location.reload()", 1000));
      
    }
  );
}
iq.select_stuff = function () {
  country_select = jQuery('#country');
  region_select = jQuery('#region');
  
  country_select.change(
    function(e) {
      new_id = jQuery(this).val();
      new_id = new_id.replace(/ /,"_");
      new_html = jQuery('#'+new_id).html();
      if(!new_html) { new_html = jQuery('#blank').html(); }
      region_select.html(new_html);
    }
  );
}
iq.change_location = function() {
  flag_links  = jQuery('a.flag');
  flag_links.click(
    function(e){
      e.preventDefault();
      new_adress = jQuery(this).attr('href');
      jQuery.post(new_adress, false,setTimeout("window.location.reload()", 1000));
 
    }
  );
}

iq.time_bar_stuff = function() {
  mytime_bar = jQuery('#time_bar div');
  
  proc = (1800 - ((60 - iq.sec) + ((29-iq.min)*60)))/1800;
  iq.new_width = (749*proc);
  mytime_bar.css('width', iq.new_width + 'px');
  
  if(iq.new_width > 249) {
    if(iq.new_width > 499) {
      if(iq.new_width > 749)
      {
        iq.safe_safe = false;
        clearInterval(count_interval);
    		document.location.replace('/finish/1');
      }
      else{
        mytime_bar.css('backgroundColor','red');
      }
      
    }
    else {
      mytime_bar.css('backgroundColor', '#119911')
    }
  }
  
}
iq.init = function(){
	iq.goTo();
	iq.count();
  iq.login_box_stuff();
  iq.animate_score();
  iq.select_stuff();
  iq.country_change_form_stuff();
  iq.change_location();
  iq.paypal_form_stuff();
}

jQuery(function(){
	iq.init();
});

