/*
//===== slider (ie: home)
var slider_speed = 5; // Note: speed in seconds
var slider_speed_counter = 0;
var slider_elems = 0;
var slider_curr = 0;
var slider_run;
function slider_move(slideTo){
	//alert(slideTo);
	slider_speed_counter = 0;
	$('div.elem_slider_btns li').each(function () {
        $(this).removeClass('on');
    });
    slider_curr = slideTo;
    $('li#slideBtn_' + slideTo).addClass('on');
    $('div.elem_slider_mask').animate({top: 0, left: -950 * slideTo}, 800);
    //alert(document.getElementById('elem_slider_mask').style.left);
}
function slider_engine(){
	slider_speed_counter++;
	if (slider_speed_counter >= slider_speed) {
		slider_speed_counter = 0;
		slider_curr++;
		if (slider_curr >= slider_elems) slider_curr = 0;
		slider_move(slider_curr);
		}		
	slider_run = setTimeout('slider_engine()', 1000);
}
*/

//===== cover story (ie: home)
var currCoverStory = 1;
var totalCoverStory = 1;
var cover_story_on = true;
var cover_story_firstrun = true;
var cover_story_engine;
function select_cover_story(elem){
    $('#coverStory_' + currCoverStory).fadeOut('fast', function() {
        $('#coverStory_btns' + currCoverStory).removeClass('on');
        $('#coverStory_btns' + elem).addClass('on');
        currCoverStory = elem;
        $('#coverStory_' + elem).fadeIn("slow");
    });
}

function run_cover_story(cntr){
    if (!cover_story_firstrun){
        tmpCoverStory = currCoverStory + 1;
        if (tmpCoverStory > totalCoverStory) tmpCoverStory = 1;
        select_cover_story(tmpCoverStory);
    }
    cover_story_firstrun = false;
    if (cover_story_on) cover_story_engine = setTimeout("run_cover_story(" + cntr + ")", cntr * 1000);
}


//===== set page up
$(document).ready(function() {
	// set navigation
	$("#page_main_nav td").each(function () {
		$(this).click(function () {
			location = $(this).find('a:first').attr('href');
		});
	});
	
	// set main search function
	$('#searchString').focus(function () {
		searchHint('searchString', 'Search', true);
	});
	$('#searchString').blur(function () {
		searchHint('searchString', 'Search', false);
	});
	
	// set links up on feat_box (home)
	$('div.feat_box li').each(function () {
		$(this).click(function () {
			location = $(this).find('a:first').attr('href');
		});
	});
	
	// set links up on cover story (home)
	if ($('coverStory_btns')){
        var i = 1;
        $('#coverStory_btns li').each(function () {
            $(this).attr("id", 'coverStory_btns' + i);
            i++;
        });
        totalCoverStory = i - 1;
        $('#coverStory_btns li div').each(function () {
            $(this).click(function () {
                tmpElem = $(this).html();
                select_cover_story(tmpElem);
                cover_story_on = false;
                clearTimeout(cover_story_engine);
            });
        });
        run_cover_story(7); //speed - in seconds
	}
	
	/*
	// slider set up (ie: home)
	slider_elems = $('div.slide').length;
	$('div.elem_slider_btns li').each(function (index) {
	    $(this).attr('id', 'slideBtn_' + index);
	    $(this).click(function () {
	    	slider_move(index);
	    });
	});
	slider_run = setTimeout('slider_engine()', slider_speed);
	*/	
});


//===== main search function
function searchHint(elem, val, onOff){
    obj = document.getElementById(elem);
    if (onOff) {
        if (obj.value == val) obj.value = '';
    } else {
        if (obj.value == '')  obj.value = val;
    }
}

//===== overlay images
(function ($) {
	// overlay template.
	var tmpl = '<img alt="" class="mask" src="/images/c.gif"/>';
	// preload the overlay image.
	new Image().src = '/images/c.gif';
	$(document).ready(function () {
		// overlay main photography
		$('#page_main_image img').each(function () {
			$(this).load(function () {
				var img = $(this),
					h = this.height,
					o = img.offset(),
					mask,
					w = this.width;
				if (!!this.src) {
					mask = $(tmpl);
					mask.
						height(h).
						width(w).
						css({
							'position': 'absolute',
							'z-index': 9,
							'top': '0px',
							'left': '2px'
						});
					img.after(mask);
				}
			});
		});
		// overlay home page carousel slides
		$('.slide img').each(function () {
			$(this).load(function () {
				var img = $(this),
					h = this.height,
					mask,
					w = this.width;
				if (!!this.src) {
					mask = $(tmpl);
					mask.
						height(h).
						width(w).
						css({
							'position': 'relative',
							'z-index': 9,
							'top': '-' + h + 'px',
							'left': '-' + w + 'px'
						});
					img.after(mask);
				}
			});
		});
	});
}(jQuery));

