$(document).ready(function(){
    // IE Hover fix
    ieHover('.vnavigation li');
    
    // Overlay
    $('.overlay').appendTo('body');    
    $('.highlightImage a[rel]').overlay({
        mask: {
            color: '#000000',
            opacity: 0.75
        },
        top: 'center'
    });   
	
    // Ticker
    var tickerWidth = $('#tickerWrapper').width() - ($('#tickerWrapper .links').width() + $('#tickerWrapper .right').width()) - 10;
    $('ul#ticker').css("width",tickerWidth+"px");
	
    $('ul#ticker').bxSlider({
        ticker:true,
        tickerSpeed: 300
    });
    
    // Description for form fields
    $("#contact1-store,#contact1-contactstore,#contact1-firstname,#contact1-lastname,#contact1-email,#contact1-telephone,#application1-job,#application1-firstname,#application1-lastname,#application1-email,#application1-telephone_mandatory,#request1-firstname,#request1-lastname,#request1-email,#request1-telephone,#order1-lenses_product, #order1-lenses_right_eye, #order1-lenses_left_eye, #order2-care_product, #order2-care_quantity_1, #order2-care_quantity_2, #order2-care_quantity_3, #order3-firstname, #order3-lastname, #order3-street, #order3-zip, #order3-city, #order3-telephone, #order3-email, #order4-store_opt, #hearingdate1-firstname, #hearingdate1-lastname, #hearingdate1-email, #hearingdate1-telephone_mandatory, #hearingdate1-date, #hearingdate1-institute_hearing, #lensesdate1-firstname, #lensesdate1-lastname, #lensesdate1-email, #lensesdate1-telephone_mandatory, #lensesdate1-date, #lensesdate1-institute_lenses, #refractiondate1-firstname, #refractiondate1-lastname, #refractiondate1-email, #refractiondate1-telephone_mandatory, #refractiondate1-date, #refractiondate1-institute_refraction1").focus(
        function() {
            $(this).parent().children('.description').show();
        }
    );
    $("#contact1-store,#contact1-contactstore,#contact1-firstname,#contact1-lastname,#contact1-email,#contact1-telephone,#application1-job,#application1-firstname,#application1-lastname,#application1-email,#application1-telephone_mandatory,#request1-firstname,#request1-lastname,#request1-email,#request1-telephone,#order1-lenses_product, #order1-lenses_right_eye, #order1-lenses_left_eye, #order2-care_product, #order2-care_quantity_1, #order2-care_quantity_2, #order2-care_quantity_3, #order3-firstname, #order3-lastname, #order3-street, #order3-zip, #order3-city, #order3-telephone, #order3-email, #order4-store_opt, #hearingdate1-firstname, #hearingdate1-lastname, #hearingdate1-email, #hearingdate1-telephone_mandatory, #hearingdate1-date, #hearingdate1-institute_hearing, #lensesdate1-firstname, #lensesdate1-lastname, #lensesdate1-email, #lensesdate1-telephone_mandatory, #lensesdate1-date, #lensesdate1-institute_lenses, #refractiondate1-firstname, #refractiondate1-lastname, #refractiondate1-email, #refractiondate1-telephone_mandatory, #refractiondate1-date, #refractiondate1-institute_refraction").focusout(
        function() {
            $(this).parent().children('.description').hide();
        } 
    );
	
    // Show stores after clicking self pickup
    $('#fieldset-order4 ol li:last').hide();
    
    $('#order4-shipping-ZustellungperPost').click(function() {
    $('#fieldset-order4 ol li:last').hide();
    });	
	
    $('#order4-shipping-AbholunginFiliale').click(function() {
    $('#fieldset-order4 ol li:last').show();
    });

    // Initialize storebox-scroller
    $(".scrollable").scrollable({vertical: true, mousewheel: false});
    
    // Remember font size
    if($.cookie('fontSize')) {
        setFontSize($.cookie('fontSize'));
    }
    $('#fontsizeSmall').click(function() {
        setFontSize('small');
    });
    $('#fontsizeMedium').click(function() {
        setFontSize('medium');
    });
    $('#fontsizeBig').click(function() {
        setFontSize('big');
    });
    
    // Touch device click/hover fix
    $('a').live('touchend', function(e) {
        var element = $(this);
        var link = element.attr('href');
        window.location = link;
    });
	
    // Add attr to a class="close"
    $('.overlay .close').attr('href', '#');
    
    // Add datePicker to certain input fields
    $('#hearingdate1-date, #lensesdate1-date, #refractiondate1-date').datePicker();
    
    //if($.os.name == 'iphone') {
    if($.browser.touch) {
	$("#order ol li label").css('float','none');
	$("#order ol li .description").css({'color': '#000A23',
									    'background': 'none',
									    'display': 'block',
										'float': 'none',
										'margin': '0',
										'padding': '0',
										'position': 'static',
										'font-size': '10px'
									   });
	$("#order ul.errors").css({'margin': '0',
							   'font-size': '10px'
							  });
	$("#order .shipping label").css('width','200px');						  
	$("#submit_order").css('margin-left','0');
	$("#order ol li .description").show();
	$("#content .formContainerPadding p").css('width','200px');
	$("#order5-comment").css('width','170px');
	
	// hearingdate, lensesdate and refractiondate forms
	
	$("#hearingdate ol li label, #lensesdate ol li label, #refractiondate ol li label").css('float','none');
	$("#hearingdate ol li .description, #lensesdate ol li .description, #refractiondate ol li .description").css({'color': '#000A23',
									    'background': 'none',
									    'display': 'block',
										'float': 'none',
										'margin': '0',
										'padding': '0',
										'position': 'static',
										'font-size': '10px'
									   });
	$("#hearingdate ul.errors, #lensesdate ul.errors, #refractiondate ul.errors").css({'margin': '0',
							   'font-size': '10px'
							  });
	$("#hearingdate .shipping label, #lensesdate .shipping label, #refractiondate .shipping label").css('width','200px');						  
	$("#submit_hearingdate, #submit_lensesdate, #submit_refractiondate").css('margin-left','0');
	$("#hearingdate ol li .description, #lensesdate ol li .description, #refractiondate ol li .description").show();
	$("#hearingdate1-message_opt, #lensesdate1-message_opt, #refractiondate1S-message_opt").css('width','170px');
	$(".dp-choose-date").css('display','none');
    }	
});

function setFontSize(size) {
    if(size == 'small') {
        $("body").css("font-size","11px");
        $("ul.metanavigation").css("font-size","10px");
        $("ul.breadcrumbs").css("font-size","10px");
        $("#ticker p").css("font-size","11px");
        $("#sitemap ul li li").css("font-size","11px");
    }
    else if(size == 'medium') {
        $("body").css("font-size","13px");
        $("ul.metanavigation").css("font-size","11px");
        $("ul.breadcrumbs").css("font-size","11px");
        $("#ticker p").css("font-size","13px");
        $("#sitemap ul li li").css("font-size","13px");
    }
    else if(size == 'big') {
        $("body").css("font-size","15px");
        $("ul.metanavigation").css("font-size","13px");
        $("ul.breadcrumbs").css("font-size","13px");
        $("#ticker p").css("font-size","15px");
        $("#sitemap ul li li").css("font-size","15px");
    }
    $.cookie("fontSize", size, { expires: 1 });
}

function ieHover(h_list) {
    $(h_list).hover(function() {
	    $(this).addClass('hover');
    }, function() {
	    $(this).removeClass('hover');
    });	
}

function showTooltip(item, id) {
    var offset = $(item).offset();
    var topOffset = offset.top-415;
    $('#'+id).css('top', topOffset+'px');
    $('#'+id).show();
}

function hideTooltip(id) {
    $('#'+id).hide();
}

function closeOverlay() {
    $('.overlay').overlay().close();
}
