﻿/*! Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 * 
 * Requires: 1.2.2+
 */

(function(b){function d(a){var f=[].slice.call(arguments,1),e=0;a=b.event.fix(a||window.event);a.type="mousewheel";if(a.wheelDelta)e=a.wheelDelta/120;if(a.detail)e=-a.detail/3;f.unshift(a,e);return b.event.handle.apply(this,f)}var c=["DOMMouseScroll","mousewheel"];b.event.special.mousewheel={setup:function(){if(this.addEventListener)for(var a=c.length;a;)this.addEventListener(c[--a],d,false);else this.onmousewheel=d},teardown:function(){if(this.removeEventListener)for(var a=c.length;a;)this.removeEventListener(c[--a],
d,false);else this.onmousewheel=null}};b.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}})})(jQuery);


/*
 * jQuery Carousel Compilation by Ryan Barr (heavily modified by Nuri)
 * http://blog.spookyismy.name/2010/05/27/customizable-and-flexible-jquery-carousel-rotator/
 * v1.0.0
 */

$(document).ready(function() {

    var rotatorInterval=null, rotatorDuration=5000;

    //Rotate Carousel, Move from li:last to li:first if necessary.
    var rotatorRotate = function(){

	    if(!rotatorNext) {

		    var $nextElement = $("#rotator-menu li.selected").next();

		    if( $nextElement.length == 0 ) {
                $nextElement = $("#rotator-menu li:first");
            }

            $nextElement.trigger("click");
	    }
    };

    /* If menu item is clicked, switch content properly. */
    $("#rotator-menu li").click(function(){

	    if ($(this).is(".selected")) {
		    return false;

	    } else {
            clearInterval(rotatorInterval); //stop rotation

		    var imgSrc = $(this).find('a.rotator').attr("href");
		    var altText = $(this).find('a.rotator').attr("title");
		    var info = "<div class='feature-title'>" + altText + "</div>" + $(this).find('.info').html();

            $(".rotator-content").animate({ opacity: 0 }, 250 , function() {
                $(".rotator-content-desc").html(info);
                $(".rotator-content-image img").attr({ src: imgSrc , alt: altText });
                $(".rotator-content").animate({ opacity: 1 }, 250 );
            });

        }
		
	    $("#rotator-menu li").removeClass('selected');
	    $(this).addClass('selected');

        rotatorInterval = setInterval(rotatorRotate, rotatorDuration); //restart rotation
        return false;    
    });


	//Pause Rotation if Hovering #rotator-menu Item
	rotatorNext = false;
	$("#rotator-menu li").hover(
		function () {rotatorNext = true},
		function () {rotatorNext = false}
	);

    //Start rotator rotation
    rotatorRotate();

});



/*  
 * Horizontal drop-down menu
 * Nuri Kevenoglu
*/
$(function(){

    $("ul.dropdown li").hover(function(){
        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible').fadeIn(menuTimeOut); //menuTimeOut is defined in the CMS/website parameters/Top Menu Roll-Over Time
    }, function(){
        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden').fadeOut(menuTimeOut);
    });
    
    $("ul.dropdown li ul li:has(ul)").find("a:first").append("<span class='more'>&raquo;</span>");

});


/*  
 * DHTML Tabs
 * Nuri Kevenoglu
*/
$(function(){

    //Tabs On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});

})


/*  
 * FancyBox popup initialization
 * Nuri Kevenoglu

 Identify and create pop-up images after page fully loads. In your HTML use a rel="galleryX" attribute in your links to mark them as popups, like this:
 <a rel="gallery1" href="full-size-image.jpg" title="Name"><img src="thumb-size-image.jpg" alt="Name" /></a>
 You can group images together using a different numeric suffix in the "gallery" value, e.g. <a rel="gallery25" .. />
 Instead o fimages, you can use any content inside the popup, including flash files. For examples, see http://fancybox.net/

*/
$(function(){

    $("a[rel^=gallery]").fancybox({ 
        'transitionIn'	: 'elastic',
	    'transitionOut'	: 'elastic',
	    'titlePosition' : 'inside',
	    'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
                            var retVal = (title.length ? title : '');
	                        if(currentArray.length>1) {
                                retVal += '<span style=\'margin-left:10px; font-size:0.8em; \'>(' + (currentIndex + 1) + ' of ' + currentArray.length + ')</span>';
                            }
                            return retVal;
	                     }
    });
      
    $("a[rel^=gallery] img").css("opacity",1.0).hover(
                function() {
                    $(this).stop().animate({ opacity: 0.8 }, 200);
                },
                function() {
                    $(this).stop().animate({ opacity: 1.0 }, 200);
                }
        );

})


/*  
 * Generic functions 
 * Nuri Kevenoglu
*/

function keycodePressed(e,keyCode) {
    if(!e) {e = window.event}; 
    var el = e.target?e.target:e.srcElement;
    //var ch=String.fromCharCode(e.keyCode);
    return keyCode==13 && el.type=='textarea' ? false: e.keyCode == keyCode;
}

function doSearch(searchTerm, searchFor){
    
    if(searchTerm && searchTerm.length>0) {
        if (searchTerm.length<3){
            alert("The search term must be at least 3 characters");
            return false;
        }
        var url = "/search/" + escape(searchTerm);
        var urchinUrl = "/metrics/user/search/" + searchTerm;
        
//        if (searchFor && searchFor.length > 0) {
//            url = url + "?f=" + searchFor
//            urchinUrl = urchinUrl + "/" + searchFor;
//        }
        urchinTracker(urchinUrl);

        window.location = url;
    }
    return false; //prevent postback
}

function printPage() {
    if (window.print) {
        window.print();
    } else {
        alert("Your browser doesn't support this feature.\nPlease try CTRL+P or CMD+P (Mac)");
    }
}

function urchinTracker(url){
    try {
        pageTracker._trackPageview(url);
    } catch (err) { }
}

function injectFlashMedia(file){

    if(file==null || file.length<7){return false}

    var assetHolderId = "#" + file.substring(file.lastIndexOf("/")+1,file.lastIndexOf(".")) + "_holder";
    if ($(assetHolderId).length == 0) {
        alert(String.format("Asset holder '{0}' is missing", assetHolderId));
        return false;
    } else if ($(assetHolderId).html().length > 0) {
        $(assetHolderId).html("");
        $(assetHolderId).hide();        
        return false;
    }

    var fileExt = file.toLowerCase().substring(file.lastIndexOf(".")+1);
    var so;

    if(fileExt=="mp3"){
        so = new SWFObject("/flash/player/audio_player.swf", "aplayer", "324", "24", "7", "#FFFFFF");

    }else if(fileExt=="flv" || fileExt=="mp4"){
        so = new SWFObject("/flash/player/video_player.swf", "vplayer", "324", "240", "7", "#000000");

    }else if(fileExt=="swf") {
        so = new SWFObject(file, "SWFdemo", "324", "240", "7", "#000000");
    }

    so.addParam("allowScriptAccess", "always");
    so.addVariable("path", file);
    so.addVariable("auto", "true");
    so.write(assetHolderId.substring(1));
    $(assetHolderId).show();        

}
