$(document).ready(function () {

  
  $('.jcarousel-skin-ainu').jcarousel({
     scroll:1,
     wrap:'last',
     animation:800
  }); 
  

  $("ul.tabs").tabs("div.panes > div", {
    current: 'active',
    tabs: 'li'    
  });

  //$("#accordion").tabs("#accordion div.pane", {tabs: 'a.openPane', effect: 'slide', initialIndex: null});
       
  $('.slideContainer').each(function (e) {
    buildCycle($(this));
  });
  
  $('.accordionRow').click(function (e) {
    e.preventDefault(); 
    $('.pane').slideUp('slow');
    
    if ($(this).find('a.openPane').hasClass('current')) {
      $(this).find('a.openPane').removeClass('current');
    }
    else {
      $('#accordion a.openPane').removeClass('current');
      $(this).find('a.openPane').addClass('current');
      $(this).next('div.pane').slideDown('slow');      
    } 
    
    /*if ($(this).hasClass('current')) {
      $(this).removeClass('current');
    }
    else {
      $('#accordion a.openPane').removeClass('current');
      $(this).addClass('current');
      $(this).parents('div.accordionRow').next('div.pane').slideDown('slow');      
    } 
    */   
  });
             
  $('#openOrder').click(function (e) {

    e.preventDefault();
    
    var defVal = $('#orderMail').attr('defaultValue');
    
    if ($('#orderMail').attr('value') == defVal) {
      var para = "";
    }
    else {
      var para = escape($('#orderMail').attr("value"));
    }
        
    var target = $(this).attr("href");
    
    return hs.htmlExpand(null, {
      dimmingOpacity: 0.75,
      objectType: 'iframe',
      outlineType: 'rounded-custom',
      src: target+'?email='+para,
      wrapperClassName: 'no-footer no-header',
      width: 760
    });       

  });
  
  $('#newsLetterColumn .customTextInput').keydown(function (e) {
  
    if (e.keyCode == 13) {
    
      $('#openOrder').click();
    }
  
  });
  
  $('#headerSearchInput').keydown(function (e) {
  
    
  
    if (e.keyCode == 13) {
    
      if ($('.wordWheelContent a.selected').length == 1) {
        
        e.preventDefault();  
      
      
        window.location = $('.wordWheelContent a.selected').attr('href');
      
        return false;   
      }
      
      
      
    }
    else if(e.keyCode == 40) {
      if ($('.wordWheelContent a.selected').length < 1) {
        $('.wordWheelContent a:first').addClass('selected');
       }
       else if ($('.wordWheelContent a.selected').next('a').length > 0){
        $('.wordWheelContent a.selected').next('a').addClass('selected');
        $('.wordWheelContent a.selected:first').removeClass('selected');     
       } 
       $(this).attr('value', $('.wordWheelContent a.selected').html());
    }

    else if(e.keyCode == 38) {
		 if ($('.wordWheelContent a.selected').prev('a').length > 0){
      $('.wordWheelContent a.selected').prev('a').addClass('selected');
      $('.wordWheelContent a.selected:last').removeClass('selected');
      $(this).attr('value', $('.wordWheelContent a.selected').html());
     }
     else {
      $('.wordWheelContent a.selected').removeClass('selected');     
     }

    }
    
    else {
    
      searchWordwheel.modified(this);
    
    } 
  
  
  });
  
  
  
// Highslide fixed popup mod. Requires the "Events" component.
if (!hs.ie || hs.uaVersion > 6) hs.extend ( hs.Expander.prototype, {
	fix: function(on) {
		var sign = on ? -1 : 1,
			stl = this.wrapper.style;

		if (!on) hs.getPageSize(); // recalculate scroll positions
		
		
		hs.setStyles (this.wrapper, {
			position: on ? 'fixed' : 'absolute',
			zoom: 1, // IE7 hasLayout bug,
			left: (parseInt(stl.left) + sign * hs.page.scrollLeft) +'px',
			top: (parseInt(stl.top) + sign * hs.page.scrollTop) +'px'
		});

		if (this.outline) {
			stl = this.outline.table.style;
			hs.setStyles (this.outline.table, {
				position: on ? 'fixed' : 'absolute',
				zoom: 1, // IE7 hasLayout bug,
				left: (parseInt(stl.left) + sign * hs.page.scrollLeft) +'px',
				top: (parseInt(stl.top) + sign * hs.page.scrollTop) +'px'
			});

		}
		this.fixed = on; // flag for use on dragging
	},
	onAfterExpand: function() {
    	this.fix(true); // fix the popup to viewport coordinates
    	
	},

	onBeforeClose: function() {
		this.fix(false); // unfix to get the animation right
	},

    onDrop: function() {
    	this.fix(true); // fix it again after dragging
	},

	onDrag: function(sender, args) {
		//if (this.fixed) { // only unfix it on the first drag event
			this.fix(true);
		//}
	}

});

  $('.customTextInput').focus(function () {
  
    var defVal = this.defaultValue;
    
    if ($(this).attr('value') == defVal) {
      $(this).attr('value','');
    }
  });
  
  $('.customTextInput').blur(function () {
  
    var defVal = this.defaultValue;      
    if ($(this).attr('value')=='') {
      $(this).attr('value',defVal);
    }
        
  });
  
  
  /*
  $('#headerSearchInput').blur(function () {
     $('.wordWheel').remove();
  });
  */ 
  
  $('#cancelOrder').click(function (e) {
    e.preventDefault();
    return parent.window.hs.close();
  
  });
  
  $('.openVideo').click(function (e) {

    e.preventDefault();
    
    var target = $(this).attr("href");
    
    return hs.htmlExpand(null, {
      dimmingOpacity: 0.75,
      objectType: 'iframe',
      outlineType: 'rounded-custom',
      src: target,
      wrapperClassName: 'no-footer no-header',
      width: 539
    });       

  }); 
  
  $('.openYoutube').click(function (e) {

    e.preventDefault();
    
    var vid = $(this).attr("rel");
    
    return hs.htmlExpand(null, {
      dimmingOpacity: 0.75,
      objectType: 'iframe',
      outlineType: 'rounded-custom',
      src: 'http://www.youtube.com/embed/'+vid,
      wrapperClassName: 'no-footer no-header',
      width: 539
    });       

  });

});  

function buildCycle(e) {
  var c_id = $(e).attr('id');
  var t_value = 0;
  var fx_value = 'fade'
  var s_value = 1000;
  
  /*if (c_id == 'neuvojaSlideContainer') {
    var t_value = 8000;
  }*/
  
  if ($(e).attr('rel') == 'autoplay') {
    t_value = 8000;
  }  
  
  if (c_id == 'productSlideContainer_pane1' || c_id == 'productSlideContainer_pane2') {
    //fx_value = 'scrollHorz';
    //fx_value = 'fade';
    return false
    //$(this).scrollable();
  }
  
  if ($('#'+c_id+' .images .slidePane').length > 1) {
   
    $('#'+c_id+' .images').cycle(
       {
        fx: fx_value,
        timeout: t_value,
        speed: s_value,
        next: $('#'+c_id + ' a.forward'),
        prev: $('#'+c_id + ' a.backward'),
        cleartype: 1,
        pause: 1  
       }
      );
    }
  else {
    $('#'+c_id+' .images .slidePane:first').css("display","block");
    $('#'+c_id + ' a.forward').css("display","none");
    $('#'+c_id + ' a.backward').css("display","none");
  }  
}

Cufon.replace('#navigation a', {
   fontFamily: 'Rockwell Std',
   hover: {
    color: '#4369ac'
   }
});  

Cufon.replace('.productItem h3 a, .productName h3 a', {
   fontFamily: 'Rockwell',
   hover: {
    color: '#4369ac'
   }
});   
 

Cufon.replace('h3.liftHeader', {
   fontFamily: 'Rockwell Std'
});  

Cufon.replace('h1, h2, .contentArea h3, #search_results h3, .newsDate span, .newsLift h3, .sortQuestions h3, .customButton span', {
   fontFamily: 'Rockwell'
});


// Add the close button
hs.registerOverlay({
//slideshowGroup: 'group1',
overlayId: 'closebutton',
position: 'top right',
fade: 2, 
useOnHtml: true
});


var eZSiteServer = "http://ainu.labra.asiakas.org";
var eZSitePath = "";
var eZImagePath = "/extension/nvberner/design/nxc_ainu/images/";


var searchWordwheel = {
	bufferText: false,
	bufferTime: 800,
	object: false,  
	data: false,
	
	modified : function(obj){
		this.object = obj;


		setTimeout('searchWordwheel.compareBuffer();', this.bufferTime);
	},
	
	compareBuffer : function(){
		strText = $(this.object).attr('value');
		if ( strText != this.bufferText && strText.length > 2){
			this.bufferText = strText;
			searchWordwheel.makeRequest();
		}
	},
	
	makeRequest : function(){

		var sval = $(this.object).attr('value');
    var requestURI = eZSitePath + "/ezjscore/call/ajaxparts::ainu_wheel::"+sval;
        
    $.get( requestURI, function(data) {
        //if( data != "" ){
          
          searchWordwheel.showResults(data);
      //} 
    })
		
		
	},
	
	showResults : function(data){
	
	  $('.wordWheel').remove();
	
	  if (data != "") {
	  
	    var jdata = JSON.parse(data);
	    
	    $(this.object).parent().prepend('<div class="wordWheel"><div class="wordWheelContent"></div><div class="wordWheelBottom"></div><div class="wordWheelBottomEnd"></div></div>');
     
      var pos = $(this.object).position();
        
      $('.wordWheel').css('left', pos.left+'px');
      $('.wordWheel').css('top', pos.top+12+'px');
  		
      $.each(jdata.Results, function(i, item) {
      
        $('.wordWheelContent').append('<a href="'+item.URL+'">'+item.Name+'</a>');
      });
    
      $('.wordWheelBottom').append('<span>'+jdata.Info[0].Footer+'</span>');
      
      $('body').click(function(){searchWordwheel.hideResults()});
      
  		/*
      $('.wordWheelContent a').click(
  			function(e) {
  			  e.preventDefault();
  				searchWordwheel.select(e, this);
  			}
  		);
      */
    }
      
    else {
      
        searchWordwheel.hideResults();
        
    }
      		
	},
	
	hideResults : function(){
		//$('body').unbind('click', searchWordwheel.hideResults);
		$('.wordWheel').remove();
		//$('.wordWheel').html('');		
		//$('.wordWheel').css('display','none');

		
	},

  select : function(e, obj){
		e.preventDefault();
		$(this.object).attr('value', $(obj).html());
	  searchWordwheel.hideResults();
	}  

	
}

