// --------------------------------------------------
// INIT 
// --------------------------------------------------


function initSubPages() {
	initSubnav();
	initLogin();
//	social();
	
	
	if (window.noCufon != true) {
			Cufon.now();
		}
	
	
	if (getFileName(location.pathname) == "index.html") {
		// FADE IN SUBNAV. DISABLED.
		//$('.catSubnav_item').css({"opacity":0});
		//animItem(0, ($('.catSubnav_item').length), $('.catSubnav_item'));
	}
	/*
	$.getJSON('images/images.js', function(data) {
		  window.imageArray = data;
		 	 $.preLoadImages(window.imageArray ,function(){
		 		if (getFileName(location.pathname) != "index.html" || window.novid == true) {
		 	 		initImageshow(window.imageArray,{"target":"#mediaContainer","height":"244px","delay":"3"});
		  		}
		  });
	});
	*/

}

function getFileName(path){
	var fn = path.match(/\/([a-z0-9_-]+\.\w+)/i);
	return (fn == null)? "" : fn[1];
}


function deleteSubnavItem(target, textIs) {
	$(target).each(function(){
		if ($(this).text() == textIs) {
			$(this).parent().remove();
		}
	})
}


function social_homepage() {
	$('.footer').after('<div id="social" class = "social"></div>');
	$('#social').jsocial({
			//	twitter		:  'ryanolson',
				facebook	:  'facebook.com/pages/UR-Channel-Digital-Signage-Multimedia-Broadcasting-Company/165637270139774?ref=ts',
			//	flickr		:  'urcmarket',
			//	delicious	:  'thatryan',
				linked		:  'linkedin.com/groups?mostPopular=&gid=3711222',
				youtube		:  'youtube.com/user/URChannelCompany',
			//	feed		:  'thatryan',
			//	friendfeed	:  'thatryan',
				digg		:  'URChannelDigitalSignageCo',
				stumbleupon	:  'stumbler/UR-ChannelCo/',
			//	lastfm		:  'thatryan',
				center		: true,	
				inline		: true,
				newPage		: true
			});
}


function social() {
	if ($('#homepageMiddleColumn').length > 0) {
		$('#homepageMiddleColumn').append('<div id="social" class = "social"></div>');
	} else {
		$('body').append('<div id="social" class = "social"></div>');
	}

	$('body').append('<div id="social" class = "social"></div>');
	$('#social').jsocial({
			//	twitter		:  'ryanolson',
				facebook	:  'facebook.com/pages/UR-Channel-Digital-Signage-Multimedia-Broadcasting-Company/165637270139774?ref=ts',
			//	flickr		:  'urcmarket',
			//	delicious	:  'thatryan',
				linked		:  'linkedin.com/groups?mostPopular=&gid=3711222',
				youtube		:  'youtube.com/user/URChannelCompany',
			//	feed		:  'thatryan',
			//	friendfeed	:  'thatryan',
				digg		:  'URChannelDigitalSignageCo',
				stumbleupon	:  'stumbler/UR-ChannelCo/',
			//	lastfm		:  'thatryan',
				center		: true,	
				inline		: true,
				newPage		: true
			});
}

function initSubnav() {
	//deleteSubnavItem('#navbucket .subNav li a', 'In the News');
	$(document).ready(function(){
		$('li.navItem').hover(function(){				//when you hover on a nav iten
		//hide all sub navs first
		$(document).find('ul.subNav').fadeOut();			

			 if (!($(this).find('ul.subNav').hasClass("active")))			//check to see if its active and if not show it
			{	
				var location = $(this);		
				var h = ($(this).offset().left - 19) + "px";	
				$(this).find('ul.subNav').css({'left':h,'position':'fixed','z-index': 1000})		
				$(this).find('ul.subNav').fadeIn();
			}
			$(this).find('ul.subNav').toggleClass("active");
		});
	$('li.navItem').mouseout(function(){
			 if (!($(this).find('ul.subNav').hasClass("active")))			//check to see if its active and if not show it
			 {
				$(document).find('ul.subNav').fadeOut();			
			 }
		});
	});
}

function initLogin() {
	$('.submitLogin').click(function(){
		$('#navbucket .subNav').css("display","none");
		alert('Submit TBA');
	});
}
// --------------------------------------------------
// GOOGLE 
// --------------------------------------------------



  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-21178203-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  
  
  

// --------------------------------------------------
// CONTACT 
// --------------------------------------------------
function contactForm(content) {
	$('#contactFormSubmit').css({"display":"none"});
	$('#emailForm').append('<div id="sending">Sending...</div>');
	
	
	 $.ajax({
		url: "http://webhost2.canbtv.com/btvdelivery/sendmail/emailsender.ashx",
		data: {
			"command": "sendemail",
			"to": 'sales@ur-channel.com',
			"subject": 'UR-channel Contact Form',
			"body": content
		},
		success: function(data) {
			contactConfirm();
			
		}
	});
}



function initContactForm() {
	$('#contactFormSubmit').click(function(){
		var linebreak = '<br>';
		//var linebreak = '\r\n';
		var name = 'Name:' + " " +  $('#cf_name').val() + linebreak;
		var company = 'Company:' + " " +  $('#cf_company').val() + linebreak;
		var phone = 'Phone:' + " " +  $('#cf_phone').val() + linebreak;
		var email = 'Email:' + " " +  $('#cf_email').val() + linebreak;
		var comments = 'Comments:' + " " +  $('#cf_comments').val();
		var content = name + company + phone + email + comments
		window.confirm = (content);
		contactForm((content));
	});
}



function contactConfirm() {
	$('#contactFormSubmit').css({"display":"block"});
	$('#sending').remove();
	$('#emailForm input, #emailForm textarea').val('');
	$('#contactConfirm').slideDown(200);
}



// --------------------------------------------------
// POPUP 
// --------------------------------------------------


function tooltip (target, message) {
	$(target).live("mouseenter", function(e){
		
			$('.tooltip').remove();
			var tooltipID = $(this).attr('id') + "_tooltip";
			var jqttid = "#"+ tooltipID;
			var tooltip = '<div class="tooltip" id="'+tooltipID+'"></div>';
			$(this).data('tooltipID', tooltipID);
			$('body').append(tooltip)
			
			$(jqttid).css({
				//"opacity":".85",
				"position":"absolute"
			});
			
			$.each(message, function(mi, mval) {
			
				var html = '<div>'+mval+'</div>';
				$(jqttid).append(html);
				
			})
			$('div', jqttid).not(':last').addClass('tt_divider');
			var thiswidth = $(jqttid).width();
			if (thiswidth > 250) {
				$(jqttid).css("width","250px")
			}	
			
			var offsetx = $(jqttid).width();
			var offsety =  20 + $(jqttid).outerHeight(true);
			mousex =  e.pageX;
			mousey = e.pageY
			if ((mousey-offsety) < 20) {
				offsety = 0
			}
			if (mousex < offsetx) {
				mousex += mousex
			}
			$(jqttid).css({
				"top":mousey-offsety,
				"left":mousex-(offsetx/2)-20
			});
	});
	
	
	$(target).live("mousemove", function(e){
			var tooltipID = $(this).data('tooltipID');
			var jqttid = "#"+ tooltipID;
			var offsetx = $(jqttid).width();
			var offsety = 20 + $(jqttid).outerHeight(true);
			mousex =  e.pageX;
			mousey = e.pageY
			if ((mousey-offsety) < 20) {
				offsety = 0
			}
			if (mousex < offsetx) {
				mousex += offsetx/2
			}
			$(jqttid).css({
				"top":mousey-offsety,
				"left":mousex-(offsetx/2)-20
			});
	});
	
	$(target).live("mouseleave", function(){
			var tooltipID = $(this).data('tooltipID');
			var jqttid = "#"+ tooltipID;
			$(jqttid).remove();
	});
}




// --------------------------------------------------
// ANIM 
// --------------------------------------------------


function animItem(i, numItems, group) {
	var  thiscolor =  $(group).eq(i).css("color");
	$(group).eq(i).animate({
		color: "#ffffff",
		opacity: 1,
		queue: "true"
	  }, 500, 'linear', function() {
	  		i++;
				if (i < numItems) {
					animItem(i, numItems, group);
				}
	  }).delay(1000)
	  .animate({
				color: '#81d7ff',
				queue: "true"
			  }, 1000, 'swing', function() {
					// done		
		}).delay(100)
	  .animate({
				color: thiscolor,
				queue: "true"
			  }, 2000, 'swing', function() {
					// done		
		});	
}


// --------------------------------------------------
// PRELOAD IMAGES 
// --------------------------------------------------

(function ($) {
	$.preLoadImages = function(imageList,callback) {
		var pic = [], i, total, loaded = 0;
		if (typeof imageList != 'undefined') {
			if ($.isArray(imageList)) {
				total = imageList.length; // used later
					for (i=0; i < total; i++) {
						pic[i] = new Image();
						pic[i].onload = function() {
							loaded++; // should never hit a race condition due to JS's non-threaded nature
							if (loaded == total) {
								if ($.isFunction(callback)) {
									callback();
								}
							}
						};
						pic[i].src = imageList[i];
					}
			}
			else {
				pic[0] = new Image();
				pic[0].onload = function() {
					if ($.isFunction(callback)) {
						callback();
					}
				}
				pic[0].src = imageList;
			}
		}
		pic = undefined;
	};

	$.preLoadCSSImages = function(callback) {
		var pic = [], i, imageList = [], loaded = 0, total, regex = new RegExp("url\((.*)\)",'i'),spl;
		var cssSheets = document.styleSheets, path,myRules,Rule,match,txt,img,sheetIdx,ruleIdx;
		for (sheetIdx=0;sheetIdx < cssSheets.length;sheetIdx++){
			var sheet = cssSheets[sheetIdx];
			if (typeof sheet.href == 'string' && sheet.href.length > 0) {
				spl = sheet.href.split('/');spl.pop();path = spl.join('/')+'/';
			}
			else {
				path = './';
			}
			myRules = sheet.cssRules ? sheet.cssRules : sheet.rules;
			for (ruleIdx=0;ruleIdx < myRules.length;ruleIdx++) {
				Rule = myRules[ruleIdx];
				txt = Rule.cssText ? Rule.cssText : Rule.style.cssText;
				match = regex.exec(txt);
				if (match != null) {
					img = match[1].substring(1,match[1].indexOf(')',1));
					if (img.substring(0,4) == 'http') {
						imageList[imageList.length] = img;
					}
					else if ( match[1].substring(1,2) == '/') {
						var p2 = path.split('/');p2.pop();p2.pop();p2x = p2.join("/");
						imageList[imageList.length] = p2x+img;
					}
					else {
						imageList[imageList.length] = path+img;
					}
				}
			};
		};

		total = imageList.length; // used later
		for (i=0; i < total; i++) {
			pic[i] = new Image();
			pic[i].onload = function() {
				loaded++; // should never hit a race condition due to JS's non-threaded nature
				if (loaded == total) {
					if ($.isFunction(callback)) {
						callback();
					}
				}
			};
			pic[i].src = imageList[i];
		}

	};
	$.preLoadAllImages = function(imageList,callback) {
		if (typeof imageList != 'undefined') {
			if ($.isFunction(imageList)) {
				callback = imageList;
			}
			else if (!$.isArray(imageList)) {
				imageList = [imageList];
			}
		}
		$.preLoadCSSImages(function(){
			if (imageList.length > 0) {
				$.preLoadImages(imageList,function(){
					callback();
				});
			}
			else {
				callback();
			}
		});
	}
})(jQuery);


/*
Plugin Name: jSocial
Version: 1.0
Plugin URI: http://thatryan.com
Description: jQuery Plugin for Social Linking
Author: Ryan Olson
Author URI: http://thatryan.com

The beautiful icons used in this plugin are by Rogie King of http://www.komodomedia.com/
*/

/* PLUGIN USAGE */
/* 
	All that is needed to display the list is an empty div with the
	class of "social"
	
		<div class="social"></div>	

	At the bottom of your page, above the </body> tag, call the script like this, 
	
	<script type="text/javascript" charset="utf-8">
	$('.social').jsocial({

			//options go in here...
	});
	</script>
	
	The available options, (for now) are as follows, for most enter only your username
	unless otherwise specified. Be sure to surround with '' . For URL's, do not enter the begining http:// 
	
	twitter		: null, 	//add your user name
	facebook	: null, 	//add your profile URL
	flickr		: null, 	//add your user name
	friendfeed	: null, 	//add your user name
	delicious	: null, 	//add your user name
	digg		: null, 	//add your user name
	lastfm		: null, 	//add your user name
	linked		: null, 	//add your profile URL
	youtube		: null, 	//add your profile URL
	feed		: null,		//add your user name
	newPage		: null,		// true or false, opens link in new page
	center		: false,	//true or false, auto center the list
	inline		: false,	//true or false, display list horizontally
	small		: false		//true or false, use small (16x16)pixel icons
	
*/


(function($) {
	
	$.fn.jsocial = function(options) {

		var options = $.extend({
			
			twitter		: null, 
			facebook	: null, 
			flickr		: null, 
			friendfeed	: null, 
			delicious	: null, 
			digg		: null, 
			lastfm		: null, 
			linked		: null, 
			youtube		: null,
			stumbleupon	: null,
			feed		: null,
			newPage		: null,
			center		: false,
			inline		: false,
			small		: false
			
		},options);
		
	
		return this.each(function() {
			
			if ( options.newPage ) {
				var target = 'target="_blank"';		//if user wants link to open in new page
			};
		
			var $this = $(this),		//cache 'this' , define variables
				total_links = 0,		//used to auto set the width of ul, for centering
				
				list = '<ul class = "social_links" style="width:100%">';	//open the list element
						
if( options.twitter ) {list += '<li ><a href="http://twitter.com/' + options.twitter + '" class="twitter" ' + target + '>Twitter</a></li>'; total_links ++;};

if( options.facebook ) {list += '<li style="background-image:none"><a href="http://' + options.facebook + '" class="facebook" ' + target + ' >Facebook</a></li>'; total_links ++;};

if( options.flickr ) {list += '<li style="background-image:none"><a href="http://flickr.com/' + options.flickr + '" class="flickr" ' + target + ' >Flickr</a></li>'; total_links ++;};

if( options.friendfeed ) {list += '<li style="background-image:none"><a href="http://friendfeed.com/' + options.friendfeed + '" class="friendfeed" ' + target + ' >FriendFeed</a></li>'; total_links ++;};

if( options.delicious ) {list += '<li style="background-image:none"><a href="http://delicious.com/' + options.delicious + '" class="delicious" ' + target + ' >Delicious</a></li>'; total_links ++;};

if( options.digg ) {list += '<li style="background-image:none"><a href="http://digg.com/' + options.digg + '" class="digg" ' + target + ' >Digg</a></li>'; total_links ++;};

if( options.lastfm ) {list += '<li style="background-image:none"><a href="http://www.last.fm/user/' + options.lastfm + '" class="lastfm" ' + target + ' >Last.FM</a></li>'; total_links ++;};

if( options.linked ) {list += '<li style="background-image:none"><a href="http://' + options.linked + '" class="linked-in" ' + target + ' >Linked-In</a></li>'; total_links ++;};

if( options.youtube ) {list += '<li style="background-image:none"><a href="http://' + options.youtube + '" class="youtube" ' + target + ' >YouTube</a></li>'; total_links ++;};

if( options.stumbleupon ) {list += '<li style="background-image:none"><a href="http://www.stumbleupon.com/' + options.stumbleupon + '" class="stumbleupon" ' + target + ' >stumbleupon</a></li>'; total_links ++;};

if( options.feed ) {list += '<li style="background-image:none"><a href="http://feeds.feedburner.com/' + options.feed + '" class="feed" ' + target + ' >Feedburner</a></li>'; total_links ++;};
			
				list += '</ul>';	//close the ul
			
			$this.append(list);	//add the list to the DOM
			
			if ( options.inline ) {
				$('ul.social_links li').css('float','left');	//add the float left to each list element
			};	//end inline if
			
			if ( options.small ) {
				$(this).removeClass('social').addClass('social_small');
			};	//end small icons if
			
			if ( options.center ) {		//if user wants social list centered
				
				if ( options.small) {	//if they chose small icons
				$('ul.social_links').css('width', '100%'); //calculate the width of the ul for centering
					
				}
				else {		//else they chose large icons
				
		$('ul.social_links').css('width', total_links*58); //calculate the width of the ul for centering
				};
				
			};	//end centering if
			
		});  //end each
	
	} //end function
	
})(jQuery);




