/*
    styling javascript for Visionect Electronics site
    requires jQuery
    Luka Birsa, 2009, (C) Visionect
*/

$(document).ready(function(){

    //fixes layout of images in sidebar
    $("#sidebar_images img:odd").css("margin-left", "10px").css("margin-right","0px");
    $("#buy_products ol li:last").css("border-bottom","none");
    $(".navigation_row_horiz ul.first").css("border-left","none");
    

    //layout
    $("#sidebar .sidebar_item:last").css("border-bottom","none");
    var tb_height=$(".text_body").height();
    var sb_height=$("#sidebar").height();
    if(tb_height<sb_height){
        $(".text_body").height(sb_height);
    }
    
    //enable jquery lightbox plugin
    $('a[rel=lightbox]').lightBox({
		imageLoading: css_base_url+'/media/jquery-lightbox-images/lightbox-ico-loading.gif',
		imageBtnClose: css_base_url+'/media/jquery-lightbox-images/lightbox-btn-close.gif',
		imageBtnPrev: css_base_url+'/media/jquery-lightbox-images/lightbox-btn-prev.gif',
		imageBtnNext: css_base_url+'/media/jquery-lightbox-images/lightbox-btn-next.gif'
    }); // Select all links that contains lightbox in the attribute rel

	//resize the content_title fancy phrase to fit
	//check .content_title_phrase css width, padding and border parms! (21 = 10+10+1)
	$('.content_title_phrase').width($('.content_title').width()-$('.content_title_text').width()-23);

	//remove last underline from text_body li
	//could be replaced later with a css3 selector:last
	$('.text_body ul').each(function(i){$(this).children('li:last').css('border-bottom','none');});
	
	//dynamic image resizing with proper aspect ratio on the first page
	$('#content-left img, #content-right img').each(function() {
		var maxWidth = 420; // Max width for the image
		var maxHeight = 1000;    // Max height for the image
		var ratio = 0;  // Used for aspect ratio
		var width = $(this).width();    // Current image width
		var height = $(this).height();  // Current image height
		// Check if the current width is larger than the max
		if(width > maxWidth){
			ratio = maxWidth / width;   // get ratio for scaling image
			$(this).css("width", maxWidth); // Set new width
			$(this).css("height", height * ratio);  // Scale height based on ratio
			height = height * ratio;    // Reset height to match scaled image
			width = width * ratio;    // Reset width to match scaled image
		}                                                                          
		// Check if current height is larger than max
		if(height > maxHeight){
			ratio = maxHeight / height; // get ratio for scaling image
			$(this).css("height", maxHeight);   // Set new height
			$(this).css("width", width * ratio);    // Scale width based on ratio
			width = width * ratio;    // Reset width to match scaled image
		}
	});

	//set the height of both content page bars to be the same
	content_diff=$('#content-left').height() - $('#content-right').height();
	a_more_margin = parseInt($('a.more').css('margin-top'));
	if(content_diff>0)
		$('#content-right a.more').css('margin-top',content_diff+a_more_margin);
	else
		$('#content-left a.more').css('margin-top',-content_diff+a_more_margin);
	
	//fade out the welcome message
	$(document).oneTime(1000,"front-title", function(){
		$("#ft1").fadeOut(1000);
		$("#ft2").fadeIn(1000);
	});

});
