/* Author:



*/

/* SIMPLE js not jq */

function hideProgress(){
        document.getElementById('frameLoader').style.display = 'none';
}

/* if I'm the top I'm the top */
if (top.location != location) {
    top.location.href = document.location.href;
  }


/* Q Pop Up Window */
function openWindow(url,link) {
popupWin = window.open(url, link, 'status=no,scrollbars=auto,resizable=yes,dependent,width=640,height=320')
popupWin.focus()
}

function openBigWindow(url,link) {
popupBigWin = window.open(url, link, 'status=no,scrollbars=auto,resizable=yes,dependent,width=640,height=400')
popupBigWin.focus()
}

/* jQuery SCRIPTS */

//$("#about-nav a").each(function() {
//	var hreflink = $(this).attr("href");

//	if (hreflink.toLowerCase() == location.href.toLowerCase()) { 
//$(this).parent("li").addClass("selected"); 
//}
//else { 
//alert(hreflink);
//}

//});

/* location.pathname only works on remote server - 0 matches root link */
/* location.href forwards the variables so that's better */

$(function(){
var path = location.href.substring(0);
var SectionNavLinks = $('#section-nav a');
   
   if ( path )
   //alert(path);
     $('#section-nav a[href$="' + path + '"]').attr('class', 'selected');
     
   	if (!SectionNavLinks.hasClass('selected')) 
    $('#section-nav a').first().attr('class', 'selected');
    
 });


/* making articles */
var SectionArticles = $("section article").hide(),
    ArticleNavLinks = $("#articles-nav li");
    ArticleNavLinks.first().addClass("active").show();
    SectionArticles.first().show();
    
    ArticleNavLinks.click(function() {
    var $this = $(this),
        activeLink = $this.find('a').attr('href');
          
    if(!$this.hasClass('active')){
        $this.addClass('active').siblings().removeClass('active');
        SectionArticles.hide().filter(activeLink).fadeIn();
    }
    return false;
});



$("#soaps").parent().css("background","url(../images/curve_fbubbles.png)");

//alert ($("#soaps").parent().get(0).id);


/* REUSABLE MODAL WINDOW */
    $(document).ready(function(){

    //get the height and width of the page
    var window_width = $(window).width();
    var window_height = $(window).height();

    //vertical and horizontal centering of modal window(s)
    /*we will use each function so if we have more then 1
    modal window we center them all*/
    $('.modal_window').each(function(){

        //get the height and width of the modal
        var modal_height = $(this).outerHeight();
        var modal_width = $(this).outerWidth();

        //calculate top and left offset needed for centering
        //var top = (window_height-modal_height)/2;
        //calculate top and left offset needed for position
        var top = (window_height-modal_height)/3;
        var left = (window_width-modal_width)/2;

        //apply new top and left css values
        $(this).css({'top' : top , 'left' : left});

    });

        $('.activate_modal').click(function(){

              //get the id of the modal window stored in the name of the activating element
              var modal_id = $(this).attr('title');

              //use the function to show it
              show_modal(modal_id);

        });

        $('.close_modal').click(function(){

            //use the function to close it
            close_modal();

        });

    });

    //THE FUNCTIONS

    function close_modal(){
        //hide the mask
        $('#mask').fadeOut(500);

        //hide modal window(s)
        $('.modal_window').fadeOut(500);
    }
    function show_modal(modal_id){
        //set display to block and opacity to 0 so we can use fadeTo
        $('#mask').css({ 'display' : 'block', opacity : 0});

        //fade in the mask to opacity 0.5
        $('#mask').fadeTo(500,0.5);

         //show the modal window
        $('#'+modal_id).fadeIn(500);

    }
    
    
/* Simple Jquery PHP Auto Slideshow with stop functions */

function theRotator(timing,rotations) {

	//Set the opacity of all images to 0
	$('#rotator li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('#rotator li:first').css({opacity: 1.0});
		
	//ap: if no timing variable use default 4000 = change to next image after 6 seconds
	if (!timing) { timing = 4000;}
	//alert (timing);
	
	//Call the rotator function to run the slideshow
	intervalID = setInterval('rotate('+rotations+')',timing);
	
	
	//ap: let's make it move to the next if the a anchor is clicked
	$('#rotator a') 
		.bind('click', function(event) {rotate(timing);});

}


var rotationcount = 1;

function rotate(rotations) {	

	//Get the first image
	var current = ($('#rotator li.show') ?  $('#rotator li.show') : $('#rotator li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#rotator li:first') :current.next()) : $('#rotator li:first'));
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
	
	
	// rotationcount++; this would count for each li in the rotation

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');

	// define first and last slide to determine which one to stop on
	var first = $('#rotator li').first().hasClass('show');
	var last = $('#rotator li').last().hasClass('show');
	
	if (first) {
	//clearInterval(intervalID);
	rotationcount++;
	}
	
	if (rotationcount == rotations) { 
	//alert ("stop - rotations equals ratationcount"); 
	clearInterval(intervalID);
	}	

};



/* a simple toggler with classes*/
$(function() {
$(".content-hidden").hide();

	$(".toggler").click(function () {
	$(".content-hidden").toggle();
	});	
});


/* make some text */
function say(text){
	$('#console') .append(text);
	}
