var currpage = 'library';
var activestate = false;
function hideoverlay() {
    dynamic_fsCommand("Library_Intro_Overlay|Close");
	$("#pl_lightBox").hide();
	$("#pl_pointersIntro").empty();
	$("#pl_pointers_container").hide();
	
}

	
function trim(s) {
	s = s.replace(/(^\s*)|(\s*$)/gi,"");
	s = s.replace(/[ ]{2,}/gi," ");
	s = s.replace(/\n /,"\n");

	return s;
}
	

function showoverlay() {
    
	$("#pl_lightBox").css({
		opacity: "0.75",
		display: "block",
		
		width: $("#pl_lightBox").parent().width()+'px'
	});
	
	var cHeight = $('body').height();
	var WHeight = $(window).height();
	if (cHeight > WHeight) {
		$("#pl_lightBox").css('height',$("#pl_lightBox").parent().height()+'px');	
	}
	
	else {
		$("#pl_lightBox").css('height',WHeight+'px');	
	}
	
	$("#pl_lightBox").show();
	
	//$("#pl_lightBox").css("border", "1px solid red");
	$("#pl_pointers_container").show().load("/us/personal/en/pointers/library/pointers_intro.html", function () {

		$("#pl_pointersIntro").css({ 
			"top": '200px',
			"left": $("#pl_container").width()/2 - $("#pl_pointersIntro").width()/2-20
		});
		
		$('#pl_pointersIntro .close_overlay').bind('click', function(){
			hideoverlay();
			
		});
	});
	
	
}


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$(document).ready(function() {

    $("#pl_lightBox").hide();
    $("#pl_pointers_container").hide();

    $('#thumbs div').live('click', function() {
        dynamic_fsCommand('Library|' + $(this).find('h4').text() + '_Click');
		
		document.location = $(this).find('span').attr('href');
        return false;

    });

    $("#pl_watchVideo").bind('click', function() {
        showoverlay();
        return false;
    });


    var cookie = $.cookie('pl_video');

    if (cookie != 'true') {

        $.cookie('pl_video', 'true', { path: '/', expires: 30 });
        showoverlay();

    }


    $(".intro_wrapper #pl_filters a").click(function(evt) {
        evt.preventDefault();

        $(this).parents("ul#pl_filters").children("li").removeClass("selected");
        $(this).parents("li").addClass("selected");
        updateCarouselContent($(this).attr('class'));

    });
    $("#thumbs div").bind('mouseenter', function() {
        $(this).addClass('hovered');
        var currclass = $(this).find('span:last').attr('class');
		
       
       

        $(this).find('span:last').removeClass(currclass);
        currclass = currclass + '_hovered';
        $(this).find('span:last').addClass(currclass);






    });
    $("#thumbs div").bind('mouseleave', function() {
        $(this).removeClass('hovered');
		
        var currclass = $(this).find('span:last').attr('class');

        $(this).find('span:last').removeClass(currclass);
        currclass = currclass.replace('_hovered', '');
        $(this).find('span:last').addClass(currclass);
    });


    /*END FILTERS*/

});
	
	function updateCarouselContent(filterClass) { 
 	
    // get handle to scrollable api 
   // var api = $("div.scrollable").scrollable(); 
    // append new item using jQuery's append() method 
	
	var p = pointers;
	
	$('#thumbs').empty()
	for (var i = 0; i < p.length; i++) {
                if (filterClass == "all" || p[i]["filterClass"].indexOf(filterClass) !== -1) {
                    $(
						"<div class='" + p[i]["filterClass"] + "'>" +
						
						"<span href='" + p[i]["linkURL"] + "' class='hrefWrapper' ></span>" +
						"<h4>" + p[i]["title"] + "</h4>" +
						"<span class='" + p[i]["imgclass"] + "'>" +
						"</span>" + 
						
						"<p>" + p[i]["desc"] + "</p>" +
						"</a></div>"
					).appendTo($('#thumbs'));
				}
			}
			
			
			  
		 $("#thumbs div").bind('mouseenter', function() {
        $(this).addClass('hovered');
        var currclass = $(this).find('span:last').attr('class');
		
       
       

        $(this).find('span:last').removeClass(currclass);
        currclass = currclass + '_hovered';
        $(this).find('span:last').addClass(currclass);






    });
    $("#thumbs div").bind('mouseleave', function() {
        $(this).removeClass('hovered');
		
        var currclass = $(this).find('span:last').attr('class');

        $(this).find('span:last').removeClass(currclass);
        currclass = currclass.replace('_hovered', '');
        $(this).find('span:last').addClass(currclass);
    });
		
    
} 
var pointers = new Array();
function getallitems() {
	
	$("#thumbs div").each(function(i) {
				
                var cTitle = $(this).find("h4");
                var cLink = $(this).find("span");
                var cImg = $(this).find("img");
                //var cHov = $(this).find("img").attr("src").substr(0, $(this).find("img").attr("src").length - 4);
                var cDesc = $(this).find("p");
				var imgClass = $(this).find('span:last');

               

                //var currHov = new Image(cImg.attr("width"), cImg.attr("height"));
                //currHov.src = currImg + "Hover.gif";
				
				
                pointers[i] = {
                    title: cTitle.text(),
                    linkURL: cLink.attr("href"),
                    //tracking: cLink.attr("name"),
                    desc: cDesc.text(),
					imgclass: imgClass.attr('class'),
                    filterClass: this.className
                }
				
            });
	}
	
	
	function introItems() {
	
		getallitems();
		
	}
	
	
	
