﻿/// <reference path="jquery.intellisense.js"/>
var newindex = -1;
var curpage = document.location.pathname;
var calledPage = '';

$(document).ready(function(){

  //$(document).pngFix();
  
  //-- page load --//
  if (document.URL.indexOf("#contentonly")>0) {
    $('.rco').remove();
    $('#content').css({
      left:0,
      paddingTop:0,
      background:'none',
      paddingLeft:0
    });
    parent.contentSlideOut();
  }
  
  //-- nav page load --//
  var nh = parseInt($('#nav').height(),10);
  var navinit = $('#nav .on').length > 0 ? $('#nav a').index($('#nav .on')) : 0;
  $('#nav').after('<div id="highlight"></div>');
  $('#highlight').css('margin-top',(navinit*33) - $('#nav').height() + 2).fadeIn(1000);
  $('.pagetitle').css('padding-top',108 + 33*navinit);
  $('.pagetitles').css({backgroundPosition:'0px -' + (navinit*24) + 'px'}).fadeIn(200);
  //$('.pagetitle #hide').css('margin-top',navinit*33);
  $('.npload').add('.npload img').removeAttr('title').removeAttr('alt');
  
  if (!jQuery.browser.msie && !(jQuery.browser.version=='6.0'))
    $('.npload').removeAttr('href');
  
  //-- load functions --//
  try {
    IELoad();
  } 
  catch (err) {}
  
  //-- events --//
  $('.npload').click(npLoadClick);
  $('.npload').hover(function() {
    try {
      $('#status').text($(this).attr('preview')).show();
    } catch (err) {}
  },function() {
    $('.footer #status').text('');  
  });
  
  $.npTooltip({
    handle:'#ContactPhone', 
    position:'right', 
    contentHtml:'919 <span class="dgreen">DOT</span> 518 <span class="dgreen">DOT</span> 0670', 
    tooltipClass:'tooltip',
    yOffset:-40,
    xOffset:-20
  });
  
});

function npLoadClick() {
  var page = $(this).attr('load');
  var page2 = page.indexOf('#')>0 ? page.substr(0,page.indexOf('#')) : page;
  newindex = $('#nav a[load="' + page + '"]').length > 0 ? $('#nav a').index($('#nav a[load="' + page + '"]')) : 0;
  calledPage = page;
  
  if ($('.oldcontent').length == 0) {
    if (page.indexOf('#')>0 && document.location.pathname.indexOf(page2)>0) {
      window.location.href = document.URL;
    }
    else if (newindex != $('#nav a').index($('#nav .on'))) {
      $('#status').text('loading page...').show(0);
      $('#content').addClass('oldcontent').removeAttr('id');
      $('.oldcontent').css({zIndex:1});
      $('.oldcontent').after('<div id="content" class="bodyright"></div>');
      
      if (page.indexOf('#contentonly')>0) {
        if (document.location.pathname.indexOf('contact/')>0) {
          window.location.href = rootpath + 'contact/';
        }
        else {
          //-- insert iframe --//
          animateContent('<iframe id="icontent" name="icontent" src="' + page + '" frameborder="0" marginheight="0" marginwidth="0"></iframe>');
        }
      } 
      else {
        $.ajax({
          type: "GET",
          url: page,
          timeout: 10000,
          success: function(data){
            //-- load page content --//
            animateContent($(data).find('#content').html());
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            $('#status').append('fail');
            setTimeout(function(){
              $('#status').hide();
            },3000);
          }
        });    
      }
      
      //-- move the jimmy jangle --//
      navmove();
    }
  }
}

function animateContent(content) {
  $('#content').html(content);
    
  //-- iframe will take care of contentSlideOut --//
  if (content.indexOf('iframe')<0) {
    contentSlideOut();
    IELoad();
  }
  
  //-- status --//
  $('#status').hide(0);
}

function contentSlideOut() {
  var speed = 500;
  if (jQuery.browser.msie) {
    //-- IE Gayness --//
    $('.oldcontent').remove();
  } else {
    //-- set up and animate --//
    $('#content').css({
      left:window.innerWidth,
      right:-window.innerWidth
    });
    $('.topfade').css('right',0);
    $('.pagetitles').fadeOut(speed);
    $('.oldcontent').animate({
      left:window.innerWidth,
      right:-window.innerWidth
    }, speed, function(){
      $('.oldcontent').remove();
      $('#content').animate({
        left:469,
        right:0
      }, speed, function(){
        if (calledPage=='index.aspx' || calledPage=='../index.aspx'){
          $('.topfade').fadeOut(200);
        } else {
          $('.topfade').fadeIn(200).css('right',20);
        }
        $('.pagetitle').css('padding-top', 108 + newindex*33);
        $('.pagetitles').css({backgroundPosition:'0px -' + (newindex*24) + 'px'}).fadeIn(speed);
      });
    });
  }
}

function navmove(){
  var time = 500;
  
  //-- move on state --//
  $('#nav .on').removeClass('on');
  $('#nav a:eq(' + newindex + ')').addClass('on');
  
  //-- animate --//
  $('#highlight').animate({
    marginTop:(newindex*33) - $('#nav').height() + 2
  },time);
//  $('.pagetitle #hide').animate({
//    marginTop:index*33
//  },time);
}

function IELoad() {
  if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
    $('.bodyright').css({
      width:document.documentElement.clientWidth - 509,
      height:document.documentElement.clientHeight
    });
  }
}
  
//  $('#nav a').hoverIntent({
//    sensitivity:2,
//    interval:200,
//    over:function(){
//      
//    },
//    out:function(){},
//    timeout:500
//  });
