var site = {

rotations : null,
index : 0,
t : null,

init : function() {

//$('.accordion').accordion({header:'h4'});

//var $tabs = $('.home #hero').tabs({
  
//select: function(ui) {
        //rotations--;
//if (!rotations) {
//$tabs.tabs({fx:{opacity:'toggle'}}).tabs('rotate', null);
//}
//}
//});

//var rotations = $tabs.tabs('length');
//$tabs.tabs({fx:{opacity:'toggle'}}).tabs('rotate', 2000);

$("#hero .slide").hide();
$("#hero .slide").eq(0).show();
$("#hero .nav li").eq(0).addClass("ui-tabs-selected");
$("#hero .nav li a").eq(0).css("background-position", "0 -10px");

$("#hero .nav li").each(function(i) {

$("a", this).eq(0).click(function(e) {

$("#hero .slide").hide();
$("#hero .nav li").removeClass("ui-tabs-selected");
$("#hero .nav li a").css("background-position", "0 0");
$("#hero .slide").eq(i).fadeIn("slow");
$("#hero .nav li").eq(i).addClass("ui-tabs-selected");
$("#hero .nav li a").eq(i).css("background-position", "0 -10px");

clearTimeout(site.t);

e.preventDefault();
});

});


site.rotations =  $("#hero .nav li").length;
site.t = setTimeout("site.updateSlide()", 7000);


// carousel
$('.home #brands .carousel').carousel({
  
beforeStart: function(before, after) {
$('.home #brands button').removeClass('disabled');
if (after.index($('.home #brands .carousel').find('li:first')) >= 0)
$('.home #brands .previous').addClass('disabled');
if (after.index($('.home #brands .carousel').find('li:last')) >= 0)
$('.home #brands .next').addClass('disabled');
},
circular: false,
visible: 4

});

$('.home #brands .previous').click(function() {
  $('.home #brands .carousel').carousel('prev');
});
$('.home #brands .next').click(function() {
  $('.home #brands .carousel').carousel('next');
});

var search_label = $('#masthead .utilities label').text();
$('#masthead .utilities input[type="text"]').val(search_label);
$('#masthead .utilities input[type="text"]').focus(function(){
if($(this).val() == search_label){
$(this).val("");
}
});
$('#masthead .utilities input[type="text"]').blur(function(){
if(!$(this).val()){
$(this).val(search_label);
}
});

$("#selected_country").click(function(e) {
  $("#country_container").show();



var theScreen =  document.createElement('div');
var theBody = document.getElementsByTagName('body')[0];

theBody.appendChild(theScreen);
theScreen.id = "screen";

$("#screen").css("height", $(document).height() + "px");
$("#screen").click(function() {
$("#country_container").hide();
$("#screen").remove();
});

e.preventDefault();
});
   

// ie6 selector support
if($.browser.msie && $.browser.version == '6.0'){
$('input[type="submit"]').addClass('submit');
$('input[type="text"]').addClass('text');
$('li:first-child').addClass('first-child');
$('#main_nav>li, #main_nav>li>a').addClass('child');
$('#main_nav>li').hover(
function(){
$(this).addClass('hover');
},
function(){
$(this).removeClass('hover');
}
);
$('#sub_nav>li, #sub_nav>li>a').addClass('child');
$('#sub_nav>li').hover(
function(){
$("ul", this).show();
},
function(){
$("ul", this).hide();
}
);
}
},

updateSlide : function() {
site.t = setTimeout("site.updateSlide()", 7000);
site.index++;

$("#hero .slide").hide();
$("#hero .nav li").removeClass("ui-tabs-selected");
$("#hero .nav li a").css("background-position", "0 0");

if(site.index < site.rotations) {
$("#hero .slide").eq(site.index).fadeIn("slow");
$("#hero .nav li").eq(site.index).addClass("ui-tabs-selected");
$("#hero .nav li a").eq(site.index).css("background-position", "0 -10px");
} else {
$("#hero .slide").eq(0).fadeIn("slow");
$("#hero .nav li").eq(0).addClass("ui-tabs-selected");
$("#hero .nav li a").eq(0).css("background-position", "0 -10px");
//clearTimeout(site.t);
site.index=0;
if(site.index < site.rotations) {
$("#hero .slide").eq(site.index).fadeIn("slow");
$("#hero .nav li").eq(site.index).addClass("ui-tabs-selected");
$("#hero .nav li a").eq(site.index).css("background-position", "0 -10px");
}
}

}

}


$(document).ready(function(){
  
if (jQuery.browser.safari && document.readyState != "complete"){
    setTimeout(site.init, 100);
    return;
  } else {

site.init();
}
});



/* MT - For print and pop up window - Added on 03/05 */
function popup_print(){
document.getElementById('printheader').innerHTML = document.getElementById('printheader_content').innerHTML;
document.getElementById('printcontent').innerHTML = document.getElementById('main_content_print').innerHTML;
window.print();
}

$(document).ready( function() {
//Get the window height and width 
var winH = $(window).height();  
    var winW = $(window).width();

//Set the popup window to center 
var cWin = $("#contentContainer"); 
    //$(cWin).css('top',  winH/2-$(cWin).height()/2);  
    $(cWin).css('left', winW/2-$(cWin).width()/2-90); 
  
    $(".ciba_box").click(function(){
   var page = $(this).attr("href");
   $(cWin).css('top',$(window).scrollTop()+10 )
   //$("#contentContainer .content, #contentContainer .page_title").empty();
   //$("#contentContainer .page_title1").html(document.getElementById('printheader_content').innerHTML);
    $("#contentContainer .page_title").html($(this).attr("title"));
  // $("#contentContainer .content").load(page);
  $("#cb-iframe").attr("src",page);
  
//  $("#cb-iframe").load( function() {
//  var iFrame = $("#cb-iframe").contents().find("head").append("<meta title='narayan'></meta>");
//console.log(iFrame);
//  });
  
  
   if($.browser.msie && $.browser.version == '6.0')
   $('#contentContainer').bgiframe();
       $(cWin).fadeIn("fast");
       return false;
    });
$("#contentContainer .close").click( function() {
$("#contentContainer").fadeOut("slow");
});

$("#emailSub").click( function() {
$.cookie("the_cookie", document.email_form.email.value, { path: '/', expires: 7 });
location.href = "/email-communication-center.shtml";
return false;
});

$("#emailContainer .close").click( function() {
$("#emailContainer").fadeOut("slow");
});

    function subscribeEmail(){
    
        var chkFilter = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/;
        var email = $("#email").val();
        
        if (email.length == 0) {
            $("#emailContainer .content").html("Email address is required!").css({color: "red",'font-weight': 'bold'
            });
            //document.getElementById("emailContent").innerHTML="Email address is required!";
        }
        else 
            if (!chkFilter.test(email)) {
                $("#emailContainer .content").html("Invalid email address").css({color: "red",'font-weight': 'bold'});
            //document.getElementById("emailContent").innerHTML="Invalid email address!";
            }
            else {
                $("#emailContainer .content").load("/subscribe-action.html")
            }
        var eWin = $("#emailContainer");
        $(eWin).css('top', $(window).scrollTop() + winH / 2 - $(eWin).height() / 2);
        $(eWin).css('left', winW / 2 - $(eWin).width() / 2 - 90);
        eWin.fadeIn("fast");
        //document.getElementById("emailContainer").style.display="block";
        //alert("Enter Trapped");
        return false;
    }

});

//Reusable pop-up layer box
$(document).ready( function() {
$("a.ciba_popup").cibadialog({width:390});
$('.backtotopdiv').click(function(){$('#masthead').ScrollTo(800);return false});
$("#aside a").click( function() {
var sId = $(this).attr('class');
if( $("#"+sId).prev(".backtotopcontainerdiv").length )
sTo = $("#"+sId).prev(".backtotopcontainerdiv");
else
sTo= $("#"+sId).prev().prev(".backtotopcontainerdiv");
$(sTo).ScrollTo(800);
return false;
});
/*flash overlay*/
$("#beauty-video").click( function(){
openDivCenter("tv-ads-overlay","closeBtnLink");
var vars = { } 
var params = {
  scale: "noScale", 
  salign: "", 
  menu: "true", 
  allowFullScreen: "true", 
  scale: "showall", 
  wmode: "transparent"
};
var attributes = { 
  id: "swfTVSpot", 
  name:"swfTVSpot" 
};
swfobject.embedSWF("/images/swf/Beauty-Of-Sight.swf", "flashcontent", "665", "370", "8.0.0.0", "", vars, params, attributes);
return false;
});

$("#inno").click( function(){
openDivCenter("tv-ads-overlay","closeBtnLink");
var vars = { } 
var params = {
  scale: "noScale", 
  salign: "", 
  menu: "true", 
  allowFullScreen: "true", 
  scale: "showall", 
  wmode: "transparent"
};
var attributes = { 
  id: "swfTVSpot", 
  name:"swfTVSpot" 
};
swfobject.embedSWF("/images/swf/cibavision-innovation.swf", "flashcontent", "665", "498", "8.0.0.0", "", vars, params, attributes);
return false;
});


/*----------------------------------------------------- for Image enhancing---------------------------------------------------*/

$("#normal").click( function(){

openDivCenter("tv-ads-overlay","closeBtnLink");
  document.getElementById("flashcontent").innerHTML ="<img src='/images/StatueLiberty/StatueLiberty_Normal.jpg' title='Clear Vision' alt='Clear Vision' width=656px;height=418px; />";
return false;
});

$("#astigmatism").click( function(){

openDivCenter("tv-ads-overlay","closeBtnLink");
document.getElementById("flashcontent").innerHTML ="<img src='/images/StatueLiberty/StatueLiberty_Astigamitsm.jpg' title='Astigamitsm' alt='Astigamitism' width=656px;height=418px; />";
return false;
});

$("#myopia").click( function(){

openDivCenter("tv-ads-overlay","closeBtnLink");
document.getElementById("flashcontent").innerHTML ="<img src='/images/StatueLiberty/StatueLiberty_Myopia.jpg' title='Myopia' alt='Myopia'  width=656px;height=418px; />";
return false;
});

$("#hyperopia").click( function(){

openDivCenter("tv-ads-overlay","closeBtnLink");
document.getElementById("flashcontent").innerHTML ="<img src='/images/StatueLiberty/StatueLiberty_Hyperopia.jpg' title='Hyperopia' alt='Hyperopia'  width=656px;height=418px; />";
return false;
});

$("#presbyopia").click( function(){

openDivCenter("tv-ads-overlay","closeBtnLink");
document.getElementById("flashcontent").innerHTML ="<img src='/images/StatueLiberty/StatueLiberty_Presbyopia.jpg' title='Presbyopia' alt='Presbyopia'  width=656px;height=418px; />";
return false;
});


/*-----------------------------------------------end of image enhancing---------------------------------------------------*/
$("#tips").click( function(){
openDivCenter("tv-ads-overlay","closeBtnLink");
var vars = { } 
var params = {
  scale: "noScale", 
  salign: "", 
  menu: "true", 
  allowFullScreen: "true", 
  scale: "showall", 
  wmode: "transparent"
};
var attributes = { 
  id: "swfTVSpot", 
  name:"swfTVSpot" 
};
swfobject.embedSWF("/images/swf/getting-started-in-soft-contact-lenses.swf", "flashcontent", "665", "487", "8.0.0.0", "", vars, params, attributes);
return false;
});

$("#tipsseealso").click( function(){
openDivCenter("tv-ads-overlay","closeBtnLink");
var vars = { } 
var params = {
  scale: "noScale", 
  salign: "", 
  menu: "true", 
  allowFullScreen: "true", 
  scale: "showall", 
  wmode: "transparent"
};
var attributes = { 
  id: "swfTVSpot", 
  name:"swfTVSpot" 
};
swfobject.embedSWF("/images/swf/getting-started-in-soft-contact-lenses.swf", "flashcontent", "665", "487", "8.0.0.0", "", vars, params, attributes);
return false;
});

});

//-------------------------------------------------------------------------------------------
// POSITION DIV IN THE CENTER OF WINDOW
//------------------------------------------------------------------------------------------
function centerLyr(divID)
{
    $('#'+divID).css("position","absolute");
 
    var newTop = ( $(window).height() - $('#'+divID).height() ) / 2+$(window).scrollTop();
    if(newTop<0) newTop = 0;

    var newLeft = ( $(window).width() - $('#'+divID).width() ) / 2+$(window).scrollLeft();
    if(newLeft<0) newLeft = 0;

    $('#'+divID).css("top", newTop + "px");
    $('#'+divID).css("left", newLeft + "px");
}

//-------------------------------------------------------------------------------------------
// RE-POSITION DIV IN THE CENTER OF WINDOW ON WINDOW RESIZE
//------------------------------------------------------------------------------------------
function rePositionLyr(status,divID) 
{
    switch(status) {
case true:
$(window).bind('resize', function() {
centerLyr(divID);
});
//scroll is not used here
//$(window).bind('scroll', function() {
//centerLyr(divID);
//});
    break;
case false:
$(window).unbind('resize');
//scroll is not used here
//$(window).unbind('scroll');
    break;
    }
}

//-------------------------------------------------------------------------------------------
// POSITION OVERLAY IN THE CENTER OF WINDOW
//------------------------------------------------------------------------------------------
function showPopupDivCenter(divID) 
{
   if($.browser.msie && $.browser.version == '6.0') $('#'+divID).bgiframe();
    var toShow = $("#"+divID); 
    toShow.css({"z-index":"9000","visibility":"visible","display":"block"});
    centerLyr(divID);
    rePositionLyr(true,divID);
}

//-------------------------------------------------------------------------------------------
// OPEN DIV OVERLAY AND POSITION CENTER
//------------------------------------------------------------------------------------------
function openDivCenter(divID,closeBtnID)
{
    var toShow="";
    // Build the background overlay div
    toShow += "<div class='site_overlay'></div>";
    $('body').append(toShow);

    $('div.site_overlay').css('height',$(document).height()).bind('click',function(){
closeDiv(divID);
return false;
    });

    $('div.site_overlay').css('opacity',0).fadeTo('slow',0.65, function(){
showPopupDivCenter(divID);
$('div.site_overlay').css("z-index","9000");
$('#'+divID).css("z-index","9500");
    });

    if(closeBtnID!=""){
        $("#"+closeBtnID).bind('click',function(){
        closeDiv(divID);
return false;
        });
    }
}

//-------------------------------------------------------------------------------------------
// CLOSE OPENED DIV OVERLAY
//------------------------------------------------------------------------------------------
function closeDiv(divID)
{
    $('div.site_overlay').remove();
    rePositionLyr(false,divID);
    var toHide = $("#"+divID); 
    toHide.css({"z-index":"0","visibility":"hidden"});
    if(divID=="all-products"){
toHide.css("width", "1px");
toHide.css("height", "1px");
    }
    if(divID=="tv-ads-overlay")
    {
$('#swfTVSpot').remove();
$('#flashcontent').remove();
$("#tv-ads-overlay").children("div.lyrRel").append("<div id='flashcontent'></div>");
    }
}

(function($){
$.fn.cibadialog = function(options) {
var $dropShadow, $cibaDialog, $cibaContent;

var defaults = {
width: 400,
height: 200,
position: 'center',
cibaZindex : 10 
};

var options = $.extend(defaults, options);
var cibaZindex = options.cibaZindex;

return this.each( function() {

var $this = $(this);

if( !$("#ciba-widget").length) {
$(['<div id="ciba-widget">',
'<div id="ciba-dialog">',
//'<div class="top">',
'<h3 class="page_title"></h3>',
//'<div class="lt"><h3 class="page_title" style="border:1px solid red;"></h3></div>',
'<div style="position:absolute; right:4px;top:4px;_top:12px;_right:12px;cursor:pointer;" class="ciba-close close"><img src="/images/ico_close.png" alt="Close" title="Close"/></div>',
//'<div class="clrboth"></div>',
//'</div>',
'<div class="content">',
'</div>',
'</div>',
'</div>'].join('')).appendTo('body');

$(".ciba-close").bind('click', function(){
$("#ciba-widget").fadeOut('slow');
return false;
});
}

var $cibaWidget = $("#ciba-widget").css({width:options.width, zIndex:cibaZindex});
$cibaDialog = $("#ciba-dialog").css({zIndex:cibaZindex});

if (!$dropShadow) {
$dropShadow = $([]);
for( var i=0;i<7; i++ ) {
$dropShadow = $dropShadow.add($('<div></div>').css({zIndex: cibaZindex-1, opacity:.1, top: 1+i, left: 1+i}));
}
$dropShadow.css({position:'absolute', backgroundColor:'#000', width:options.width}).attr("class","shdw");
$dropShadow.prependTo($cibaWidget);
}

$cibaContent = $("#ciba-dialog .content");
 
$this.bind('click', function(event){
var page = $this.attr('href');
if( !$cibaWidget.is(':hidden')) {
$cibaWidget.hide();
}
$.ajax({
url: page,
context: $cibaContent,
success : function(data, textStatus) {
$cibaContent.html(data);
var title = $cibaContent.find('h3').text();
$("#ciba-widget .page_title").html(title);
$("#ciba-dialog .content h3").remove();
$cibaWidget.css({top:event.pageY + 15,left:event.pageX}).fadeIn('fast');
var cHeight = $cibaDialog.height();
$dropShadow.css({height:cHeight+35});
}
});
return false;
});

});
}
})(jQuery);

//////////////////// scroll ////////////////////////////////////////////////////////////////////////////
jQuery.getPos = function (e)
{
var l = 0;
var t  = 0;
var w = jQuery.intval(jQuery.css(e,'width'));
var h = jQuery.intval(jQuery.css(e,'height'));
var wb = e.offsetWidth;
var hb = e.offsetHeight;
while (e.offsetParent){
l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
e = e.offsetParent;
}
l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e)
{
if (e) {
w = e.clientWidth;
h = e.clientHeight;
} else {
w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
}
return {w:w,h:h};
};
jQuery.getScroll = function (e) 
{
if (e) {
t = e.scrollTop;
l = e.scrollLeft;
w = e.scrollWidth;
h = e.scrollHeight;
} else  {
if (document.documentElement && document.documentElement.scrollTop) {
t = document.documentElement.scrollTop;
l = document.documentElement.scrollLeft;
w = document.documentElement.scrollWidth;
h = document.documentElement.scrollHeight;
} else if (document.body) {
t = document.body.scrollTop;
l = document.body.scrollLeft;
w = document.body.scrollWidth;
h = document.body.scrollHeight;
}
}
return { t: t, l: l, w: w, h: h };
};

jQuery.intval = function (v)
{
v = parseInt(v);
return isNaN(v) ? 0 : v;
};

jQuery.fn.ScrollTo = function(s) {
o = jQuery.speed(s);
return this.each(function(){
new jQuery.fx.ScrollTo(this, o);
});
};

jQuery.fx.ScrollTo = function (e, o)
{
var z = this;
z.o = o;
z.e = e;
z.p = jQuery.getPos(e);
z.s = jQuery.getScroll();
z.clear = function(){clearInterval(z.timer);z.timer=null};
z.t=(new Date).getTime();
z.step = function(){
var t = (new Date).getTime();
var p = (t - z.t) / z.o.duration;
if (t >= z.o.duration+z.t) {
z.clear();
setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
} else {
st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
z.scroll(st, sl);
}
};
z.scroll = function (t, l){window.scrollTo(l, t)};
z.timer=setInterval(function(){z.step();},13);
};

