var ie6 = (navigator.appVersion.match(/MSIE [0-6]\./));

function popup( movieurl, width, height, title ) {
  $("#popup").remove();
  var str = '<div id="popup"><a class="close"><span>close</span></a>'
  +'<table><tr><td class="cell1x1"></td><td class="cell1x2"><h1>'+title+'</h1></td><td class="cell1x3"></td></tr>'
  +'<tr><td class="cell2x1"></td><td class="cell2x2" style="width:'+width+'px; height:'+height+'px;">'
  +'<iframe src="'+movieurl+'" name="video" scrolling="no" frameborder="0" width="'+width+'" height="'+height+'"/>'
  +'</td><td class="cell2x3"></td></tr><tr><td class="cell3x1"></td><td class="cell3x2"></td><td class="cell3x3"></td></tr>'
  +'</table></div>';
  $('body').append( str );
  $('#popup a.close').click( function() { $('#popup').remove(); } );
  var leftpos = (document.body.offsetWidth - width - 73 - 48 ) / 2;
  var toppos = document.documentElement.scrollTop; 
  $('#popup').css({top: toppos, left: leftpos});
  return false;
}
function smallpopup( imgurl, width, height, title, desc ) {
  $("#small-popup").remove();
  var str = '<div id="small-popup"><a class="close"><span>close</span></a>'
  +'<div class="inner"><img src="'+imgurl+'" width="'+width+'" height="'+height+'" alt=""/>'
  +'<div class="desc"><h3>'+title+'</h3><p>'+desc+'</p></div></div></div>';
  $('body').append( str );
  $('#small-popup .inner').css({'width': width});
  $('#small-popup a.close').click( function() { $('#small-popup').remove(); } );
  var leftpos = (document.body.offsetWidth - $('#small-popup').width() - 100) / 2;
  var toppos = document.documentElement.scrollTop; 
  $('#small-popup').css({top: toppos, left: leftpos});
  return false;
}

function moveToLeft() {
  $('#slideshow ul').animate({scrollLeft:'-=179'});
  return false;
}
function moveToRight() {
  $('#slideshow ul').animate({scrollLeft:'+=179'});
  return false;
}
jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++)
    jQuery("<img>").attr("src", arguments[i]);
}
var currimage = 0;
if (!homeimages) { var homeimages = []; };
function initMovie() {
  if (($('body.home #image').length > 0) && (homeimages.length > 0)) {
    preloadImage();
    setTimeout('countdownImage()',10000);
  }
}
var imgtime = 0; 
function countdownImage() {
  nextImage();
  preloadImage();
  setTimeout('countdownImage()',10000);
}
function showArrows() {}
function hideArrows() {}
function preloadImage() {
  nextimage = (currimage+1 >= homeimages.length) ? 0 : currimage + 1;
  $.preloadImages( homeimages[nextimage].pic );
  return false;
}
function nextImage() {
  imgtime = 0;
  currimage = (currimage+1 >= homeimages.length) ? 0 : currimage + 1;
  showImage( currimage );
  imgtime = 0;
  return false;
}
function prevImage() { }
function showImage( nr ) {
  $('#image img').fadeOut(250, function() {
    $('#image a#url').addClass('loading');
    $("#image img").load( function() {
      $('#image a#url').removeClass('loading').attr('href',homeimages[nr].url);
      $("#image img").fadeIn(800); 
    }).attr({src: homeimages[nr].pic});
  }); 
}

function showRedHover() {
  var backimg = $(this).children('img');
  newsrc = backimg.attr('src').replace('.png','hover.png');
  $(this).append( '<img class="front" style="opacity:0.0;filter:alpha(opacity=0);" src="'+newsrc+'"/>' );
  $(this).children('img').animate({marginTop:'-99px'},{queue:false,duration:300});
  $(this).children('img.front').animate({opacity: 1.0},{queue:true,duration:300});
}
function hideRedHover() {
  $(this).children('img').animate({marginTop:'0px'},{queue:false,duration:300});
  $(this).children('img.front').animate({opacity: 0.0},{queue:true,duration:300}, function() { $(this).remove() } );
}

$(document).ready(function(){
  $('#slider a.left').click( moveToLeft );
  $('#slider a.right').click( moveToRight );
  $('#slideshow li a').bind('mouseenter',showRedHover).bind('mouseleave',hideRedHover);
  // preload buttons image
  $('#slideshow li img').each( function() { 
    newsrc = $(this).attr('src').replace('.png','hover.png');
    $.preloadImages( newsrc );
  });
  $('div.product').click( function() { 
    window.location = $(this).children('a.wrap').attr('href'); 
  });
  $('div.review div.description').each( function() {
    var h = $(this).height();
    if (h > 50) {
      $(this).css({height:'4em',overflow:'hidden'})
      $(this).attr('old_height',h);
      $(this).parent('div.review').append('<a class="readmore" href="#"><span>read more</span></a>');
    }
  });
  $('div.review a.readmore').bind('click', function() {
    var rev = $(this).parent('div.review')
    var desc = rev.children('div.description')
    if (rev.hasClass('expanded')) {
      desc.animate({height:'4em'},'fast');
    } else {
      var h = desc.attr('old_height');
      desc.animate({height:(h+'px')},'fast');
    }
    rev.toggleClass('expanded');
    return false;  
  }); 
  if (ie6) {
    $('input').each( function() {
      var tp = $(this).attr('type');
      switch(tp){
        case 'text': 
        case 'radio':
        case 'button': $(this).addClass( tp ); break;
        case 'submit': $(this).addClass('button'); break;
        case 'checkbox': $(this).addClass('check'); break;
      }
    });
  }
  initMovie();
});