
jQuery.noConflict();

jQuery(document).ready(function() {	

	jQuery('#optionsbtn').toggle(function(){
	  jQuery('#optionswrap').animate({ 
        left: "0px"
      }, 300 );
	}, function(){
	  jQuery('#optionswrap').animate({ 
        left: "-200px"
      }, 300 );
	});
    
    function setColor(hex) {
		jQuery('#colorSelector div').css('backgroundColor', '#' + hex);
		
		jQuery('.subheaderbanner').css('backgroundColor', '#' + hex);
		
		jQuery('#footerwrap').css('backgroundColor', '#' + hex);
		
		jQuery('.ddsmoothmenu ul li ul li a').hover(function(){
			jQuery(this).css('background', '#' + hex);
		},
		function(){
			jQuery(this).css('background', 'transparent');
		});
		
		jQuery('.buttonlight').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){});
		
		jQuery('.buttondark').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){
			jQuery(this).css('backgroundColor', '#444444');
		});
		
		jQuery('#search .searchbutton').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){
			jQuery(this).css('backgroundColor', 'transparent');
		});
		
		jQuery('.blogpages li a').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){});
		
		jQuery('.blogpages li .selected').css('backgroundColor', '#' + hex);
		
		jQuery('.addreply').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){});
		
		jQuery('.sendmessage').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){});
		
		jQuery('.sendnews').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){});
								
		jQuery('a.portfoliobutton').css('backgroundColor', '#' + hex);
		
		jQuery('.portfoliobutton').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){});
		
		jQuery('.portfoliobutton_noselect').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){});
								
		jQuery('.postinfo a:link').css('color', '#' + hex);
		jQuery('.postinfo a:visited').css('color', '#' + hex);
		jQuery('.postinfo a').hover(function(){
			jQuery(this).css('color', '#999');
		},
		function(){
			jQuery(this).css('color', '#' + hex);
		});
		
		jQuery('.blogcategories a:link').css('color', '#' + hex);
		jQuery('.blogcategories a:visited').css('color', '#' + hex);
		jQuery('.blogcategories a').hover(function(){
			jQuery(this).css('color', '#999');
		},
		function(){
			jQuery(this).css('color', '#' + hex);
		});
		
		jQuery('.sidebar .popularbloglist a:link').css('color', '#' + hex);
		jQuery('.sidebar .popularbloglist a:visited').css('color', '#' + hex);
		jQuery('.sidebar .popularbloglist a').hover(function(){
			jQuery(this).css('color', '#999');
		},
		function(){
			jQuery(this).css('color', '#' + hex);
		});
		
		jQuery('.contacttable a:link').css('color', '#' + hex);
		jQuery('.contacttable a:visited').css('color', '#' + hex);
		jQuery('.contacttable a').hover(function(){
			jQuery(this).css('color', '#999');
		},
		function(){
			jQuery(this).css('color', '#' + hex);
		});
        
        jQuery('.timestamp  a:link').css('color', '#' + hex);
		jQuery('.timestamp  a:visited').css('color', '#' + hex);
		jQuery('.timestamp  a').hover(function(){
			jQuery(this).css('color', '#999');
		},
		function(){
			jQuery(this).css('color', '#' + hex);
		});
        
        jQuery('.postertext  a:link').css('color', '#' + hex);
		jQuery('.postertext  a:visited').css('color', '#' + hex);
		jQuery('.postertext  a').hover(function(){
			jQuery(this).css('color', '#999');
		},
		function(){
			jQuery(this).css('color', '#' + hex);
		});
        
        jQuery('.comment-reply-link').hover(function(){
			jQuery(this).css('backgroundColor', '#' + hex);
		},
		function(){});
        
        jQuery('.formpart  a:link').css('color', '#' + hex);
		jQuery('.formpart  a:visited').css('color', '#' + hex);
		jQuery('.formpart  a').hover(function(){
			jQuery(this).css('color', '#999');
		},
		function(){
			jQuery(this).css('color', '#' + hex);
		});
    
        
	}
							
	jQuery('#colorSelector').ColorPicker({
		color: '#327aa3',
		onShow: function (colpkr) {
			jQuery(colpkr).fadeIn(500);
			return false;
		},
		onHide: function (colpkr) {
			jQuery(colpkr).fadeOut(500);
			return false;
		},
		onChange: function (hsb, hex, rgb) {
			setColor(hex);
		}
	});
	
	jQuery('.le_blue').mousedown(function() {
		jQuery('#colorSelector').ColorPickerSetColor('#327aa3');
		setColor('327aa3');
	});
	jQuery('.le_orange').mousedown(function() {
		jQuery('#colorSelector').ColorPickerSetColor('#d46300');
		setColor('d46300');
	});
	jQuery('.le_green').mousedown(function() {
		jQuery('#colorSelector').ColorPickerSetColor('#718500');
		setColor('718500');
	});
	jQuery('.le_red').mousedown(function() {
		jQuery('#colorSelector').ColorPickerSetColor('#850007');
		setColor('850007');
	});
	jQuery('.le_violet').mousedown(function() {
		jQuery('#colorSelector').ColorPickerSetColor('#850047');
		setColor('850047');
	});
	jQuery('.le_dark').mousedown(function() {
		jQuery('#colorSelector').ColorPickerSetColor('#424242');
		setColor('424242');
	});
	
	jQuery('.pattern1').mousedown(function() {
		jQuery('body').css('background-image', "url('http://newline.damojothemes.com/wp-content/themes/newline/images/tiles/pattern1.gif')");
	});
	jQuery('.pattern2').mousedown(function() {
		jQuery('body').css('background-image', "url('http://newline.damojothemes.com/wp-content/themes/newline/images/tiles/pattern2.gif')");
	});
	jQuery('.pattern3').mousedown(function() {
		jQuery('body').css('background-image', "url('http://newline.damojothemes.com/wp-content/themes/newline/images/tiles/pattern3.gif')");
	});
	jQuery('.pattern4').mousedown(function() {
		jQuery('body').css('background-image', "url('http://newline.damojothemes.com/wp-content/themes/newline/images/tiles/pattern4.gif')");
	});
	jQuery('.pattern5').mousedown(function() {
		jQuery('body').css('background-image', "url('http://newline.damojothemes.com/wp-content/themes/newline/images/tiles/pattern5.gif')");
	});
	jQuery('.pattern6').mousedown(function() {
		jQuery('body').css('background-image', "url('http://newline.damojothemes.com/wp-content/themes/newline/images/tiles/pattern6.gif')");
	});

	/* Anything Slider */
	jQuery('#slider').anythingSlider({
		resizeContents      : false,
		addWmodeToObject    : 'transparent',
		buildArrows 		: false,
		buildStartStop		: false,
		buildNavigation		: true,
		hashTags			: false,
		autoPlay            : true,
		delay               : 5000	});
				      
	/* Hover image opacity */
	jQuery("a[data-rel^='fadeimg'] img").hover(function() {
		jQuery(this).stop().fadeTo("fast", 0.5); 
	},function(){
		jQuery(this).stop().fadeTo("fast", 1.0); 
	});
    
    /* Menu */
	ddsmoothmenu.init({
		mainmenuid: "smoothmenu1", //menu DIV id
		orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
		classname: 'ddsmoothmenu', //class added to menu's outer DIV
		contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
	});
	
	/* Social Tooltip */
	jQuery('#headersocial img[title]').tooltip({opacity: 0.5, effect: 'fade', delay: 0, fadeOutSpeed: 0, fadeInSpeed: 0});
		
	/* Teaser Carousel */
	if(jQuery('#hometeasers').length != 0){
		jQuery('#hometeasers').tinycarousel({ display: 3, interval: false, intervaltime: 5000, duration: 600});
	}
    	
    /* Footer Container Element Sizing */
	jQuery('#footercontainer div:nth-child(4)').addClass("middlewidget");
	
	/* Content Tabs */
	jQuery("ul.tabs").tabs("div.panes > div");
	
	/* Sidebar Height */
	setSidebarHeight();
    
     /* Sidebar Remove Last Widget Divider */
	jQuery('.sidebar div:last').remove('.widgetdivider');
	
	/* Pricing Table Alternate Row Color */
	jQuery('.pricingtable td:odd').css({background: '#e5e5e5'});
	
	/* Contact Form */
	if(jQuery('#contactform').length != 0){
		addForm('#contactform');
	}
	
	/* Newsletter Subscription */
	if(jQuery('#newsletterform').length != 0){
		addForm('#newsletterform');
	}
	
	/* Blog Comments */
	if(jQuery('#replyform').length != 0){
		addForm('#replyform');
	}
	
	/* Blog & Portfolio Social Sharing */
	addTwitter();
	addFacebook();
	
	/* PrettyPhoto */
	addPrettyPhoto();
	
	/* Portfolio Quicksand */
	addPortfolio('1');
	addPortfolio('3');
 
});

jQuery(window).load(function() {

	/* Nivo Slider */
	if(jQuery('#nivoSlider').length != 0){
		jQuery('#nivoSlider').nivoSlider({
			effect:'random', // Specify sets like: 'fold,fade,sliceDown'
			slices:15, // For slice animations
			boxCols:8, // For box animations
			boxRows:4, // For box animations
			animSpeed:500, // Slide transition speed
			pauseTime:3000, // How long each slide will show
			startSlide:0, // Set starting Slide (0 index)
			directionNav:false, // Next & Prev navigation
			directionNavHide:true, // Only show on hover
			controlNav:true, // 1,2,3... navigation
			controlNavThumbs:false, // Use thumbnails for Control Nav
			controlNavThumbsFromRel:false, // Use image rel for thumbs
			controlNavThumbsSearch: '.jpg', // Replace this with...
			controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
			keyboardNav:true, // Use left & right arrows
			pauseOnHover:true, // Stop animation while hovering
			manualAdvance:false, // Force manual transitions
			captionOpacity:1, // Universal caption opacity
			prevText: '&larr;', // Prev directionNav text
			nextText: '&rarr;', // Next directionNav text
			beforeChange: function(){}, // Triggers before a slide transition
			afterChange: function(){}, // Triggers after a slide transition
			slideshowEnd: function(){}, // Triggers after all slides have been shown
			lastSlide: function(){}, // Triggers when last slide is shown
			afterLoad: function(){} // Triggers when slider has loaded
		});
	}
	
	/* Google Maps */
	loadGoogleMaps();

	/* Skype Toolbar Removal */
	removeSkype();
    
});


/* Linkify and Relative Time functions by Ralph Whitbeck http://ralphwhitbeck.com/2007/11/20/PullingTwitterUpdatesWithJSONAnd$.aspx */
String.prototype.linkify = function() {
        return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
                return m.link(m);
        });
};

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  var r = '';
  if (delta < 60) {
        r = 'a minute ago';
  } else if(delta < 120) {
        r = 'couple of minutes ago';
  } else if(delta < (45*60)) {
        r = (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (90*60)) {
        r = 'an hour ago';
  } else if(delta < (24*60*60)) {
        r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
        r = '1 day ago';
  } else {
        r = (parseInt(delta / 86400)).toString() + ' days ago';
  }
  
  return r;
}

function setSidebarHeight(){
	if((jQuery('#contentcolumn').height()) > jQuery('.sidebar').height()){
		jQuery('.sidebar').height(jQuery('#contentcontainer').height()-10);
	}	
}

function addPrettyPhoto() {
	/* PrettyPhoto init */
	jQuery("a[data-rel^='prettyPhoto']").prettyPhoto({
		theme: 'pp_default',
		overlay_gallery: false,
		show_title: false,
		hideflash: true
	});
	
	/* PrettyPhoto hover image opacity */
	jQuery("a[data-rel^='prettyPhoto'] img").hover(function() {
		jQuery(this).stop().fadeTo("fast", 0.5); 
	},function(){
		jQuery(this).stop().fadeTo("fast", 1.0); 
	});	
}

function removeSkype() {
	/* Skype Toolbar Removal */
	var script = document.createElement('meta');
	script.setAttribute('name','SKYPE_TOOLBAR');
	script.setAttribute('content','SKYPE_TOOLBAR_PARSER_COMPATIBLE');
	jQuery('head').append(script); 
}

function addFacebook() {
	var fbscript = document.createElement('script'); 
	fbscript.type = 'text/javascript'; 
	fbscript.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1'; 
	document.getElementsByTagName('head')[0].appendChild(fbscript);
	jQuery(document).find('.facebookwrap').each(function() {
		var btnurl = jQuery(this).attr('data-url');
		jQuery(this).append('<fb:like href="'+ btnurl +'" layout="button_count" show_faces="false" colorscheme="light" width="90"></fb:like>'); 	
	})
}

function addTwitter() {
	jQuery(document).find('.twitterwrap').each(function() {
		var btntext = jQuery(this).attr('data-text');
		var btnurl = jQuery(this).attr('data-url');
		jQuery(this).append('<a href="http://twitter.com/share" class="twitter-share-button" data-text="'+ btntext +'" data-url="'+ btnurl +'" data-count="horizontal" data-via="Newline">&nbsp;</a><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>');
	})
}

function initGoogleMaps() {
	/* Google Maps Init */
	var myLatlng = new google.maps.LatLng(50.935816, 6.961212);
	var myOptions = {
		zoom: 14,
		center: myLatlng,
		popup: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("googlemap"), myOptions);
	
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		title: "Our Office Location"
	});
	google.maps.event.addListener(marker, 'click', function() {
		map.setZoom(17);
	});
}
  
function loadGoogleMaps() {
	/* Google Maps Load */
	if(jQuery('#googlemap').length != 0){
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initGoogleMaps";
		document.body.appendChild(script);
	}
}

function addPortfolio(portfolioid) {
	/* Portfolio Quicksand */
	var $list = jQuery('#portfoliolist'+portfolioid+'column');
	
	if(jQuery($list).length != 0){
		
		var $data = $list.clone();

		jQuery('.portfoliofilter li').click(function(e) {
	
			jQuery(".portfoliofilter li a").addClass("portfoliobutton_noselect");
			jQuery(".portfoliofilter li a").removeClass("portfoliobutton");	
			jQuery(this).children('a').removeClass("portfoliobutton_noselect");
			jQuery(this).children('a').addClass("portfoliobutton");
			
			var filterVal = jQuery(this).children('a').text().toLowerCase().replace(' ','-'); 

			if(portfolioid==1){
				if (filterVal == 'all') {
					var $filteredData = $data.find('.blogpost');
				} else {
					var $filteredData = $data.find('.blogpost[data-type~=' + filterVal + ']');
				}
			}else{
				if (filterVal == 'all') {
					var $filteredData = $data.find('.portfolio');
				} else {
					var $filteredData = $data.find('.portfolio[data-type~=' + filterVal + ']');
				}
			}
			
			jQuery($list).quicksand($filteredData, {
				duration: 500,
				easing: 'swing',
				adjustHeight: 'dynamic',
				enhancement: function() {
					jQuery('.sidebar').height("100%");
				}
			}, function(){
				addPrettyPhoto();
				setSidebarHeight();
				jQuery(document).find('.twitterwrap').each(function() {
					jQuery(this).empty();
				})
				addTwitter();
				jQuery(document).find('.facebookwrap').each(function() {
					jQuery(this).empty();
				})
				addFacebook();
			});
			
			return false;
		});
	}
}

/* Contact Form */
function addForm(formtype) {

	var formid = jQuery(formtype);
	var emailsend = false;
	
	formid.find("button[name=send]").click(sendemail);
	
	function validator() {
		
		var emailcheck = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		var othercheck = /.{4}/;
		var noerror = true;
		
		formid.find(".requiredfield").each(function () {
													 
			var fieldname = jQuery(this).attr('name');
			var value = jQuery(this).val();

			if(fieldname == "email"){
				if (!emailcheck.test(value)) {
					jQuery(this).addClass("formerror");
					noerror = false;
				} else {
					jQuery(this).removeClass("formerror");
				}	
			}else{
				if (!othercheck.test(value)) {
					jQuery(this).addClass("formerror");
					noerror = false;
				} else {
					jQuery(this).removeClass("formerror");
				}	
			}
		})
		
		if(!noerror){
			formid.find(".errormessage").fadeIn();
		}
		
		return noerror;
	}
	
	function resetform() {
		formid.find("input").each(function () {
			jQuery(this).val("");	
		})
		formid.find("textarea").val("");
		emailsend = false;
	}
	
	function sendemail() {
		formid.find(".successmessage").hide();
		var phpfile = "";
		if(formtype=="#contactform"){
			phpfile = "php/contact.php";
		}else if(formtype=="#newsletterform"){
			phpfile = "php/signup.php";
		}else{
			phpfile = "";
		}
		if (validator()) {
			if(!emailsend){
				emailsend = true;
				formid.find(".errormessage").hide();
				formid.find(".sendingmessage").show();
				jQuery.post(phpfile, formid.serialize(), function() {
					formid.find(".sendingmessage").hide();
					formid.find(".successmessage").fadeIn();
					resetform();
				});
			}
		} 
		return false
	}
}
