var item = 1;
var left = null;
var itemRight= 1;
var leftLeft = null;

$(document).ready(function() {

  if ($.browser.opera) {
    $('.bx-auto').attr('style', 'margin-top: -32px !important;');
  }

  $('.box-navigation ul li').find('a').click(function() {
    var action = $(this).attr('href');
    $(this).parent('li').parent('ul').find('li').removeClass('selected');
    $(this).parent('li').addClass('selected');
    if($(action).length==1) {
      $('body').find('div.box-content div.content > div').hide(0);
      $('body').find('div.box-content div.content > div.specification-box').fadeIn(100);
      $('body').find(action).fadeIn(100);
    }
    return false;
  });

  $('.references-left > ul > li').each(function(index, domEle) {
    var indexLeft = index * $(domEle).width();
    $(domEle).attr('style', 'position: absolute; left: '+indexLeft+'px');
  });
  $('.references-right > ul > li').each(function(index, domEle) {
    var indexLeft = index * $(domEle).width();
    $(domEle).attr('style', 'position: absolute; right: '+indexLeft+'px');
  });

  function animateListRight (num) {
    $('.references-right > *').show(0);
    $('.references-right > ul').animate({
      right : -num
    }, 100, function() {
      if(leftLeft==null) {
        leftLeft = $('.references-right > ul > li').length * $('.references-right > ul > li:first-child').width();
      }
      animateListRight(num + 2);
      var elementPosition = $('.references-right > ul > li:first-child').width();
      elementPosition = elementPosition * itemRight;
      var listPosition = $('.references-right > ul').css('right');
      if(-elementPosition > parseInt(listPosition.replace(/px/, ''))) {
        $('.references-right > ul').append('<li style="position: absolute; right: '+leftLeft+'px; height: '+$('.references-right > ul > li:first-child').height()+'px">' + $('.references-right > ul > li:first-child').html() + '</li>');
        $('.references-right > ul > li:first-child').remove();
        itemRight = itemRight + 1;
        leftLeft = parseInt(leftLeft) + parseInt($('.references-right > ul > li:first-child').width());
      }
    });
  }
  function animateListLeft (num) {
    $('.references-left > *').show(0);
    $('.references-left > ul').animate({
      left : -num
    }, 100, function() {
      if(left==null) {
        left = $('.references-left > ul > li').length * $('.references-left > ul > li:first-child').width();
      }
      animateListLeft(num + 2);
      var elementPosition = $('.references-left > ul > li:first-child').width();
      elementPosition = elementPosition * item;
      var listPosition = $('.references-left > ul').position().left;
      if(-elementPosition > listPosition) {
        $('.references-left > ul').append('<li style="position: absolute; left: '+left+'px; height: '+$('.references-left > ul > li:first-child *').height()+'px">' + $('.references-left > ul > li:first-child').html() + '</li>');
        $('.references-left > ul > li:first-child').remove();
        item = item + 1;
        left = parseInt(left) + parseInt($('.references-left > ul > li:first-child').width());
      }
    });
  }

  $('.specification-more-link a').click(function() {
    if($(this).attr('href')!="#" && $(this).attr('href')!="javascript:void(0)" && $(this).attr('href')!="javascript:void(0);") { return true; }
    if($(this).attr('data-attr') == "map") {
      submitPopUp.show_map();
    } else if($(this).attr('data-attr') == "contact") {
      var post_id = "contact";
      var form_layout = $(this).attr('data-formlayout');
      submitPopUp.show_contact(post_id, form_layout);
    } else {
      var title = $('h3.title').html();
      var post_id = $('p.product-id').html();
      var id = $(this).attr('data-formlayout');
      submitPopUp.show(title, id, post_id);
    }
  });
  $('.more-actions a[data-type="forward"]').click(function() {
    var title = $(this).attr('data-title');
    var post_id = $(this).attr('data-id');
    var url = $(this).attr('data-url');
    submitPopUp.show_forward(title, url, post_id);
  });
  $('.more-actions a[data-type="print"]').click(function() {
    var title = $(this).attr('data-title');
    var id = $(this).attr('data-id');
    var post_id = $(this).attr('data-id');
    submitPopUp.show_print(title, id, post_id);
  });
  if($('body.popup h5.title').length==1) {
    var title = invertReplaceSpaceWithControlChar($('h5.title').text());
    $('h5.title').text(title);
/*     alert(invertReplaceSpaceWithControlChar(title)); */
    //
    $('input[name=your-subject]').val(title);
    // $('input[name=your-subject]').attr('disabled', 'disabled');
  }
  if($('body.popup input[type="hidden"][name="url"]').length==1) {
   var url = $('body.popup input[type="hidden"][name="url"]').val();
    $('input[name=your-url]').val(url);
    // $('input[name=your-url]').attr('disabled', 'disabled');
  }
  $('div#footer').click(function(event) {
    window.location.href="/";
  });
  if($('.references-right').length==1) {
    animateListRight(1);
  }
  if($('.references-left').length==1) {
    animateListLeft(1);
  }

  if($('body.popup.print h3.title').length > 0) {
    var title = invertReplaceSpaceWithControlChar($('h3.title').text());
    $('h3.title').text(title);
  }
  if($('body.popup.print .box-content > .content > div.left').length>0) {
    $('body.popup.print .box-content > .content > div.left').each(function(index, domEle) {
      var id = $(this).attr('id').replace(/#/g, "");
      if(id == "benefit") {
        id = "Benefit";
      } else if (id == "network") {
        id = "Network integration";
      } else if (id == "features") {
        id = "Features";
      } else {
        return false;
      }
      $(this).prepend("<h3 class=\"sub-title\">"+id+"</h3>");
    });
  }

  // form clearer
  $("body.popup input[type=submit]").val(null);

  // form input clearer
  var oldSearchStr = new Array();

  $('input[type=text]').focus(function() {
    var name = $(this).attr('name');
    oldSearchStr[name] = $(this).val();
    $(this).val(null);
  });
  $('input[type=text]').blur(function() {
    var name = $(this).attr('name');
    if($(this).val()=="" && oldSearchStr[name]) {
      $(this).val(oldSearchStr[name]);
     }
  });
  $('input[type=password]').focus(function() {
    $(this).val(null);
  });
});

function replaceSpaceWithControlChar(string) {
  ret = string.replace(/\s/g, "_").replace(/\&amp\;/g, 'ampersand');
  return ret;
}

function invertReplaceSpaceWithControlChar(string) {
  return string.replace(/\_/g, " ").replace(/ampersand/g, '&');
}


var submitPopUp = {
  show: function(headline, id, post_id, userHost) {
    if(userHost==null) {
      userHost = "undefined";
    }

    var browser_headline = headline.replace(/\s/g, "-").replace(/&/g, 'and');

    if(id!=null) {
      myWindow = window.open(window.location.protocol+"//"+window.location.host+"?page_id=70&post_id="+post_id+"&form_layout="+id+"&host="+userHost+"&ip="+userIp, "Product","height=640,width=610,scrollbars=1,resizable=0");
    } else {
      myWindow = window.open(window.location.protocol+"//"+window.location.host+"?page_id=70&post_id="+post_id+"&host="+userHost+"&ip="+userIp, "Product","height=600,width=610,scrollbars=1,resizable=0");
    }
  },
  show_map: function() {
    window.open(window.location.protocol+"//"+window.location.host+'?page_id=72&type=map','Opencode_Map','height=640,width=600,scrollbars=1,resizable=0');
  },
  show_forward: function(headline, url, post_id) {

    //var encoded_url = replaceSpaceWithControlChar(headline);

    window.open(window.location.protocol+"//"+window.location.host+'?page_id=72&type=forward&post_id='+post_id+'&form_layout=307&url='+url,'Send_as_Email','height=600,width=630,scrollbars=1,resizable=0');
  },
  show_contact: function(post_id, form_layout) {

    //var encoded_url = replaceSpaceWithControlChar(headline);
    window.open(window.location.protocol+"//"+window.location.host+'?page_id=70&type=forward&post_id='+post_id+'&form_layout='+form_layout,'Send_as_Email','height=600,width=630,scrollbars=1,resizable=0');

  },
  show_print: function(headline, post_id) {

    var regExpAsz = /[^a-zA-z0-9]/g;
    var browser_headline = headline.replace(regExpAsz, "_");

    window.open(window.location.protocol+"//"+window.location.host+'?page_id=72&type=print&post_id='+post_id,browser_headline+'_Printable_Version','height=600,width=800,scrollbars=1,resizable=0');

    //window.open(window.location.protocol + "//" + window.location.host + '?page_id=72&type=print&title='+encoded_url+'&post_id='+post_id,browser_headline+'_Printable_Version','height=600,width=800,scrollbars=1,resizable=0');
  }
}
