$$ = (new function() {
   var $$ = this, $ = jQuery, _cart;

   $$.constructor = undefined;
   
   $$.dialog = function(options) {
      var div = $('<div></div>');
      
      if(typeof options.content == 'undefined')
         return;
      
      div.append(options.content);
   
      options.close = function() {
         $(this).remove();
      }
      
      div.dialog(options);
   }

   $$.formatNumber = function(n, dec, dsep, tsep) {
      var x = n.toFixed(dec).split('.');
      return x[0].replace(/\d{1,3}(?=(\d{3})+)/g, '$&' + tsep) + 
         (typeof x[1] != 'undefined' ? (dsep + x[1]) : ''); 
   }
   
   $$.formatPrice = function(price) {
      return $$.formatNumber(price, 0, ',', ' ');
   }
   
   $$.updateCart = function(x) {
   }
/*   
   $$.removeFromCart = function(id) {
      $.ajax({
         url: '/__ajax/remove_from_cart.php',
         data: { id: id },
         cache: false,
         dataType: 'json',
         type: 'GET',
         success: function(x) {
            if((typeof x.errno == 'undefined') || x.errno)
               return;
               
            $('#p_' + id).remove();
            
         }
      });
            
   }
*/   
   $$.activateBox = function(id, code) {
      $.ajax({
         url: '/__ajax/confirm_box.php',
         type: 'GET',
         dataType: 'json',
         cache: false,
         data: { id: id, code: code },
         success: function(x) {
            if(typeof x.errno == 'undefined')
               return;
            
            if(x.errno) {         
               $$.dialog({
                  content: 
                     '<p class="tiny">' + x.error + '</p>',
                  title: 'Fel',
                  resizable: false,
                  modal: true,
                  buttons: {
                     'Ok': function() {
                        $(this).dialog('close');
                     }
                  }
               });
               
               return;
            }

            if(x.data.ptype == 44) { /* change to 4 to activate pk */
               location = '/kundvagnen';
            }
            
            else {
               $$.dialog({ 
                  content: 
                     'Du kommer nu att slussas vidare till Live its websida',
                  title: 'Tack',
                  resizable: false,
                  modal: true,
                  buttons: {
                     'Ok': function() {
                        location = liveit_url + '/boxinfo?SID=' + x.sid; 
                        $(this).dialog('close');
                     }
                  }
               });
            }
            
            /*
               
            _box = box;

            if ((box.ptype == 0) || ((box.ptype == 3) && (box.prod_id != 0)))
               loadContent('pages/boxinfo.php', null);
            */
         }
      });
   }
   
   $$.newsletterSubscribe = function(i_email) {
      var email = $('#'+i_email)[0].value;
      var foretag = false;
      var opt_in = true;
      var site = 2;
      var data = { email : email, foretag : foretag, opt_in : opt_in, site : site };
      
      if(!email || typeof email == 'undefined') return;
         
      $.ajax({
         url: '/__ajax/newsletter_subscribe.php',
         data: data,
         dataType: 'json',
         type: 'get',
         success: function(x) {
        	if(x.errno){
        		$$.dialog({
                	modal: true,
                  title: 'Error',
                	content: 'Ett fel inträffade<br /><br />'+x.error
                }); 
        	}
        	else{
	            $$.dialog({
	               modal: true,
                  title: 'Tack',
	               content: 'Din e-postadress '+email+' har registrerats!'
	            });
        	}
         }
      });         
   }
   
   $$.addToCart = function(type, id, qty, show_error) {
      var data = {};
      
      if(type == 'standard')
         data = { tag: 'standard', id: id, quantity: qty };
      else if(type == 'voucher')
         data = { tag: 'voucher', value: id, quantity: qty };
      else
         return;
      
      if(show_error === undefined)
         show_error = true;
      
      $.ajax({
         url: '/__ajax/add_to_cart.php',
         data: data,
         cache: false,
         dataType: 'json',
         type: 'GET',
         success: function(x) {
            
            if(typeof x.errno == 'undefined')
               return;
            
            if(x.errno) {
               $$.dialog({
                  content: 
                     '<p class="tiny">' + (show_error ? x.error : 
                        'Ett fel inträffade när boxen skulle läggas till') + '</p>',
                  title: 'Handla vidare',
                  resizable: false,
                  modal: true,
                  buttons: {
                     'Ok': function() {
                        $(this).dialog('close');
                     }
                  }
               });
            }
            
            else {
               $$.updateCart(x);
               
               $$.dialog({
                  content: 
                     '<p class="tiny">Produkten har lagts till i kundvagnen.</p>' +
                     '<p class="tiny">Vill du fortsätta handla eller gå till kundvagnen?</p>',
                  title: 'Handla vidare',
                  resizable: false,
                  modal: true,
                  buttons: {
                     'Gå till kundvagnen': function() {
                        location = '/kundvagnen';
                     },
                     'Fortsätt handla': function() {
                        location.reload();                         
                     }
                  }
               });
            }
         }
      });
   }
   
})

Content = new function() {
   var _this = this;
   this.constructor = null;
   
   this.showProduct = function(id) {
      
      id = parseInt(id);
      
      if(id < 100000)
         id += 90000;
      
      var matches = /(\?.*)$/.exec(location);

      var v = matches ? matches[1] : '';
      
      location = '/upplevelser/' + id + v;
   }
}

function popupOpen(url, title, width, height)
{
	//CREATED BY NIKLAS
	// popup_container
	// popup_box
	// popup_fader = background fader
   // Modified/quickfix by Richard. Added URL fetching functionality. Perhaps this should be 
   // rewrite to use the Utils.js popup script instead?
	
	var popup_container = document.createElement('div');
	popup_container.setAttribute('id','popup_container');
	document.body.appendChild(popup_container);
	
	$('#popup_container').html('<div id="popup_fader"></div><div id="popup_box" style="width:'+width+'px;"><div id="popup_title">'+title+'<div id="popup_close"><a href="#" onClick="javascript:popupClose()"><img src="/ico/cancel16.png" /></a></div></div></p><iframe src="' + url + '" frameborder="0" style="position:relative;width:100%;height:100%;"/>');
	
	$('#popup_container').css('width','100%')
				.css('height','100%')
				.css('position','fixed')
				.css('top','0')
				.css('left','0')
				.css('z-index','400');
	$('#popup_fader').css('width','100%')
				.css('height','100%')
				.css('background-color','black')
				.css('position','absolute')
				.css('opacity','0.3')
				.css('top','0')
				.css('left','0')
				.css('z-index','-1');
	$('#popup_box').css('position','absolute')
				.css('height',height+'px')
				.css('background-color','white')
				.css('left','50%')
				.css('margin-left','-'+width/2+'px')
				.css('margin-top','200px');
	$('#popup_box iframe').css('width',width-15+'px')
				.css('height',height-35+'px')
				.css('margin-left','10px');
	$('#popup_title').css('background-color','red')
				.css('padding','6px')
				.css('margin','3px')
				.css('font-size','12px')
				.css('color','white')
				.css('font-weight','bold');
	$('#popup_close').css('float','right');
	
   //window.open(url, 'popup',  'status = 0, height = ' + height + ', width = ' + width + ', resizable = 0');
}


function popupClose(){
	document.body.removeChild(document.getElementById('popup_container'));
}

