/* 
	BLOX CORE EDITORIAL APPLICATION COMMON JS
	Last Update: 1/15/2009
	Author: Ryan Davis <rdavis@qctimes.com>
*/

// cookie plugin
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('r.5=w(k,d,a){4(m d!=\'H\'){a=a||{};4(d===p){d=\'\';a.3=-1}2 g=\'\';4(a.3&&(m a.3==\'n\'||a.3.u)){2 f;4(m a.3==\'n\'){f=G E();f.C(f.B()+(a.3*z*s*s*v))}o{f=a.3}g=\'; 3=\'+f.u()}2 b=a.7?\'; 7=\'+(a.7):\'\';2 e=a.9?\'; 9=\'+(a.9):\'\';2 l=a.t?\'; t\':\'\';6.5=[k,\'=\',K(d),g,b,e,l].I(\'\')}o{2 h=p;4(6.5&&6.5!=\'\'){2 c=6.5.F(\';\');D(2 i=0;i<c.8;i++){2 j=r.A(c[i]);4(j.q(0,k.8+1)==(k+\'=\')){h=y(j.q(k.8+1));x}}}J h}};',47,47,'||var|expires|if|cookie|document|path|length|domain|||||||||||||typeof|number|else|null|substring|jQuery|60|secure|toUTCString|1000|function|break|decodeURIComponent|24|trim|getTime|setTime|for|Date|split|new|undefined|join|return|encodeURIComponent'.split('|'),0,{}))

$(function() {

        // tooltips
        $('.tn-tooltip').tooltip({
	    track: true,
	    delay: 0,
	    showURL: false,
	    fade: 250
        });

	// profanity filter and replace
	$(".profanity").text("[expletive deleted]");

	// header definitions
	var leeMenu = '#blox-main-nav li';
	var leeMenuSelected = 'selected-nav';
	var leeSearchMenu = '#blox-search-nav';
	var searchTextEl = '#blox-search-form .search-bar';
	
	// externals
	$('a[rel="external"]').click(function(){
          window.open($(this).attr('href'));
          return false;
        });
	
	// set homepage
	$('#makeHomePage').click(function(){
		if(window.showModalDialog){
			setHomePage($(this).attr('href'));
			window.open($(this).attr('href'));
		} else {
			alert('Please drag this link onto home icon!');
		}
		return false;
	});
	
	// lets look for toggle elements within the menu
	var isToggleMenu = $(leeMenu + ' .nav-toggle').length > 0 ? true : false;
	
	// global nav toggle
	function toggleMainNav(lObj){
		var nSelected = $(lObj).attr('class');
		$(leeMenu + '.' + leeMenuSelected).removeClass(leeMenuSelected);
		if(nSelected!=leeMenuSelected){
			$(lObj).addClass(leeMenuSelected);
		} else if(!isToggleMenu){
			$(lObj).addClass(leeMenuSelected);
		}
	}
	
	// add hover event
	if(!isToggleMenu){
		$(leeMenu + ' ,' + leeMenu+ 'span').mouseover(function(){
			toggleMainNav(this);
    	});
	}

	// add click event
	$(leeMenu + ' em.nav-toggle a, '+ leeMenu + ' em.nav-toggle-over a').click(function(){
		toggleMainNav($(this).parent().parent());
		return false;
    });

	// login-dialog-popup (using ui.dialog.js | ui.draggable.js | ui.resizeable.js)
		//$.ui.dialog.defaults.bgiframe = true;

	/****
	$('#login-dialog-popup').dialog({ autoOpen: false });
		$('.login-link').click(function() {
				$('#login-dialog-popup').dialog('open');
		});
	
	$('#logout-dialog-popup').dialog({ autoOpen: false });	
		$('.logout-link').click(function() {
				$('#logout-dialog-popup').dialog('open');
		});
	*** BN removed 6/1/09 **/

	// search menu
	$(leeSearchMenu + ' a').click(function(){
		var sType = $(this).attr('title');
		if(sType){
			switch(sType){
				case 'stories':
					$('#blox-search-form').attr('action','/search/');
				break;
				case 'web':
					$('#blox-search-form').attr('action','/search/web/');
				break;
				case 'classifieds':
					$('#blox-search-form').attr('action','/search/classifieds/');
				break;
			}
			$(leeSearchMenu + ' li a').removeClass(leeMenuSelected);
			$(this).addClass(leeMenuSelected);
			$(leeSearchMenu + ' #type').attr('value',sType);
			return false;
		}
	});
	
	
	// screen tabs
	$('#most_popular > ul').tabs();
	$('#market_place > ul').tabs();
	$('#blox-calendar-widget > ul').tabs({ fx: [null, { width: 'show' }] });
	
	// center piece widget
	function centerPieceDisplay(carousel)
	{
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
		
		// add center piece navigation
		if(carousel.size() > 1){
			var nStr = '';
			for(i=1; i<=carousel.size(); i++){
				nStr += '<a id="center-piece-page_'+i+'" href="#'+i+'">'+i+'</a>';
			}
			$('#blox-center-piece ol').before('<div id="center-piece-nav"></div><div class="clear"></div>');
			$('#center-piece-nav').html(nStr);
		}
		
		// add paging event
		$('#center-piece-nav a').bind('click', function() {
			carousel.scroll($.jcarousel.intval($(this).text()));
			carousel.startAuto(0);
			return false;
		});
	};
	
	// init callback for centerpiece paging
	function cpPaging(carousel, state)
	{
		$('#center-piece-nav a').removeClass('selected');
		cpSelected = '#center-piece-page_'+carousel.first;
		$(cpSelected).addClass('selected');
	};
	
	// story photo gal
	$('#blox-story-photos-page, #blox-story-photos-children, #blox-story-photos-keywords, #blox-story-photos-sections').jcarousel({scroll: 6,animation: 'slow' });
	// story photo click event
	$('#blox-story-photos-page a, #blox-story-photos-children a, #blox-story-photos-keywords a, #blox-story-photos-sections a').click(function(){

		var thisImgHost = $('#siteHost').attr('title');
		var galTitle = $(this).attr('title');
		var galCutline = $(this).attr('name');
		var galPhoto = $(this).attr('rel');
		var galPhotoHires = galPhoto.replace('.image.','.hires.');
		
		$('h2#gallery-title').html(galTitle);
		$('#blox-story-photos-page a, #blox-story-photos-children a, #blox-story-photos-keywords a,#blox-story-photos-sections a').attr('src',galPhoto);
		// chang photo attributes
		$('h2#gallery-title').html(galTitle);
		$('#blox-large-photo-page #img-holder').attr('src',galPhoto);
		$('#gallery-cutline').html(galCutline);
		$('#blox-large-photo-page #img-holder').parent().attr('href',galPhoto);
		$('.blox-thumb-container a').removeClass('selected');
		// add selected class
		$(this).addClass('selected');
		return false;
	});
	
	// carousels
	$('#blox-slideshow-widget li, #blox-story-photos-page li, #blox-story-photos-children li, #blox-story-photos-keywords li, #blox-story-photos-sections li, #blox-slider-featured li, #blox-center-piece li,.blox-ticker li').show();
	$('#blox-slideshow-widget').jcarousel();

	// $('#blox-story-photos-page, #blox-story-photos-children, #blox-story-photos-keywords, #blox-story-photos-sections').jcarousel({scroll: 6,animation: 'slow' });
	// $('#blox-story-video-page').jcarousel({scroll: 6,animation: 'slow' });

	$('#blox-slider-featured').jcarousel({scroll: 2,animation: 'slow' });
	$('#blox-center-piece ol').jcarousel({
        auto: 10,
		scroll: 1,
		animation: 'slow',
		buttonNextHTML: null,
		buttonPrevHTML: null,
        wrap: 'last',
		itemLoadCallback: cpPaging,
        initCallback: centerPieceDisplay
    });
	$('.blox-ticker').jcarousel({
        auto: 6,
		scroll: 1,
		animation: false,
		buttonNextHTML: null,
		buttonPrevHTML: null,
        wrap: 'last'
    });

	// load yahoo content match
	if($('#yahoo-contentmatch').length > 0){
		$.ajax({
			type: "GET",
			url: "/app/yahoo/contentmatch/index.php",
			data: "cat=" + $('#ycm_url_path').html() + '&ctxtUrl=' + location.href.escape,
			dataType: "html",
			success: function(result){ $('#yahoo-contentmatch').html(result); }
		});
	}
	
	// load topad rss feeds
	$('.get-feed').each(function(){ 
		eval($(this).attr('title'));
		if(!feedLimit){ var feedLimit=''; }
		var rId = $(this).attr('id');
		if(rId && feedUrl){
			$.get("/app/scripts/rss/get.php", { type:feedType,limit:feedLimit,feed:feedUrl }, 
				function(result){ 
					$('#'+rId).html(result); 
				}
			);
		}
	});
	
	// get cookie
	var $cookie_name = 'sitename-FontSize';
	var originalFontSize = $('#blox-story-text').css('font-size');
	
	// if exists load saved value, otherwise store it
	if($.cookie($cookie_name)) {
		var $getSize = $.cookie($cookie_name);
		$('#blox-story-text').css({fontSize : $getSize + ($getSize.indexOf('px')!=-1 ? '' : 'px')}); // IE fix for double "pxpx" error
	} else {
		$.cookie($cookie_name, originalFontSize);
	}
		
	// Increase Font Size
	$('#large').click(function(){
		var currentFontSize = $('#blox-story-text').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.2;
		$('#blox-story-text').css('font-size', newFontSize);
		return false;
	});
	// Decrease Font Size
	$('#default').click(function(){
		var currentFontSize = $('#blox-story-text').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.8;
		$('#blox-story-text').css('font-size', newFontSize);
		return false;
	});

	// peel hack
	if($('#blox-peel-ad').length > 0){

		$('#blox-peel-container embed')
		.attr('allowScriptAccess','always')
		.attr('wmode','transparent')
		.attr('height','75')
		.attr('width','75');
		
		var peelHTML = $('#blox-peel-container div').html();
		
		$('#blox-peel-ad').html(peelHTML);
	}
// end doc ready
});

// peel ad expand
function peelAd(w,h){
	$('#blox-peel-ad embed').attr('height',w).attr('width',h);
}
