$(document).ready(function() {
	$('.styled-select, ').customStyle();	
	//$('#main-articles, .sidebar').equalHeightColumns();
	
	$(".accordion").accordion({ 
		autoHeight: false,
		active: 0,
		create: function(event, ui){
			$('ul.accordion').find('a.active').parent().removeClass('ui-state-default').addClass('ui-state-active');
		}
	});
	
	// Load monthly archives from select box
	$("#archives-select").change(function(){
		window.location = $(this).val();									  
	});

	// Force external links to open on a new window
	/*$("a[href^='http:']:not([href*='" + window.location.host + "'][target='_blank'])").click(function(){
  		$(this).attr('target','_blank'); 
 	});  */
	
	// Override twitter plugin follow link
	$(".tweet-tools").empty();
	$(".tb_tweet a").live('mouseover', function(){
		//alert('d');									 
	});
	
	if($("#post-slider").length > 0){
		$('#post-slider').jcarousel({
			wrap: 'circular',
			scroll: 1
		});
	}
	
	// Form placeholder
	if(!$.support.placeholder) { 
		var active = document.activeElement;
		$(':text').focus(function () {
			if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
				$(this).val('').removeClass('hasPlaceholder');
			}
		}).blur(function () {
			if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
			}
		});
		$(':text').blur();
		$(active).focus();
		$('form').submit(function () {
			$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
		});
	}
	
	// Twitter refresh links. Works with the Tweet Blender plugin being used.
	$("a.refresh-link").click(function(){
		
		$.each(jQuery('form.tb-widget-configuration'),function(i,obj){

			var widgetConfId = obj.id,
			widgetId,
			widgetHTML,
			needWidgetHTML = false;
			
			// if there is widget HTML div following the form we don't need to build HTML
			if ($('#'+obj.id).next().length > 0) { 
				if ($('#' + obj.id).next().attr('id') != '') {
					if ($('#' + obj.id).next().attr('id').indexOf('-mc') > 0) {
						widgetId = widgetConfId.substr(0, widgetConfId.length - 2);
					}
					else {
						needWidgetHTML = true;
						widgetId = widgetConfId;
					}
				}
				else {
					needWidgetHTML = true;
					widgetId = widgetConfId;
				}
			}
		});
		
		TB_blend(widgetId);
		console.log('test');
		return false;					   
	});
	
	if ($.browser.webkit) {
		$('.customStyleSelectBoxInner').css({'width':'260px'}); // Fix styled select arrow position
	}
	
	//Tracking Codes
	$('a.trackclick').click(function(){
		var s = s_gi('Comm'); // Call the variable

		// Get the text from the data-tracking attribute or from the html within the a tag
		if(($(this).attr('data-tracking')) && ($(this).attr('data-tracking') != "")){
			var c = $(this).attr('data-tracking');
		}else{
			var c = $(this).html();
		}
		
		// Set internal or external link
		var comp = new RegExp(location.host);
		if(comp.test($(this).attr('href'))){         
			var l = 'o'; // Internal Link
		}else{
			var l = 'e'; // External Link
		}	
		s.tl(this,l,c);
	});
	
	$("#commentform").submit(function(){
		//trackclick($(this));	
		var s = s_gi('Comm'); // Call the variable
		s.linkTrackVars='prop36'; 
		s.prop36=s.pageName; /*Visitor Comments */
		s.tl(this,'o',$(this).attr("data-title")+" Comments");
		//return false;
	});
	
	$(".IN-widget").find('a').live('click',function(){
		trackclick($(this));				 
	});
	
	function trackclick(t){
		var s = s_gi('Comm'); // Call the variable
		parent = t.parent().parent().parent().parent().parent();
		if(parent.is('.social-menu')){
			me = "header";
		}else{
			me = "article";
		}
		s.tl(this,'e','Linkedin|'+me);
	}
	
	
	$(".carousel-link").click(function(){
		var s = s_gi('Comm'); // Call the variable	
		var t = $(this).attr('title');
		s.linkTrackVars='prop41'; 
		s.prop41=t; /*Tabbed Content*/
		s.tl(this,'o',t);
	});
});


(function($) {
    /* Custom Selectbox */
    $.fn.extend({

        customStyle: function(options) {
            if (!$.browser.msie || ($.browser.msie && $.browser.version > 6)) {
                return this.each(function() {

                    var currentSelected = $(this).find(':selected');
                    $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">' + currentSelected.text() + '</span></span>').css({ position: 'absolute', opacity: 0, fontSize: $(this).next().css('font-size') });
                    var selectBoxSpan = $(this).next();
                    var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) - parseInt(selectBoxSpan.css('padding-right'));
                    var selectBoxSpanInner = selectBoxSpan.find(':first-child');
                    selectBoxSpan.css({ display: 'inline-block' });
                    selectBoxSpanInner.css({ width: selectBoxWidth, display: 'inline-block' });
                    var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
                    $(this).height(selectBoxHeight).change(function() {
                        selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   // (Dan) This was not ideal
                        selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
                        // Thanks to Juarez Filho & PaddyMurphy
                    });

                });
            }
        }
    });

})(jQuery);
