// --- gaTracker: jQuery Google Analytics Integration // A quicker, automated way to embed Google Analytics. // (c)2007 Jason Huck/Core Five Creative
(function($){$.gaTracker=function(code,opts){opts=jQuery.extend({external:'/external/',mailto:'/mailtos/',download:'/downloads/',extensions:['pdf','doc','xls','csv','jpg','gif','mp3','swf','txt','ppt','zip','gz','dmg','xml']},opts);function decorateLink(u){var trackingURL='';if(u.indexOf('://')==-1&&u.indexOf('mailto:')!=0){var ext=u.split('.')[u.split('.').length-1];var exts=opts.extensions;for(i=0;i<exts.length;i++){if(ext==exts[i]){trackingURL=opts.download+u;break;}}}else{if(u.indexOf('mailto:')==0){trackingURL=opts.mailto+u.substring(7);}else{var regex=/([^:\/]+)*(?::\/\/)*([^:\/]+)(:[0-9]+)*\/?/i;var linkparts=regex.exec(u);var urlparts=regex.exec(location.href);if(linkparts[2]!=urlparts[2])trackingURL=opts.external+u;}}
return trackingURL;}
function addTracking(){_uacct=code;urchinTracker();$('a').each(function(){var u=$(this).attr('href');if(typeof(u)!='undefined'){var newLink=decorateLink(u);if(newLink.length){$(this).click(function(){urchinTracker(newLink);});}}});}
function initGA(){try{var gaURL=(location.href.indexOf('https')==0?'https://ssl':'http://www');gaURL+='.google-analytics.com/urchin.js';$.getScript(gaURL,function(){addTracking();});}catch(err){console.log('Failed to load Google Analytics:'+err);}}
initGA();}})(jQuery);

function resizetheimages(){
	$('#maincontent ul li a img').each(function(){
		$(this).css({'width':'','height':''});	
		
		var oldwidth = $(this).width();
		var oldheight = $(this).height();
		var newwidth = $(this).parent().width();
		
		if(newwidth<oldwidth){
			var newheight = oldheight * ( newwidth / oldwidth ); newheight = Math.ceil(newheight);
		};
		
		if(newwidth>=oldwidth){
			var newwidth = oldwidth;
			var newheight = oldheight;
		};
		
		$(this).css({'width':newwidth,'height':newheight});	
	});	
};

$(document).ready(function(){
	$.gaTracker('UA-5121746-6');
	if(!$.browser.msie){ var isIE = '1'; };
	//resizetheimages();
	$('.jshide').show();

	var uploadformz = '<form enctype="multipart/form-data" action="http://guyswithiphones.com/cgi-bin/mt/mt-cp.cgi" method="POST" id="upform"><fieldset><label for="customfield_submitteremail"><strong>Email</strong><span> (required)</span></label><input id="customfield_submitteremail" name="customfield_submitteremail" class="name_text"></fieldset><fieldset><label for="customfield_submittername"><strong>Name</strong><span> (required)</span></label><input id="customfield_submittername" name="customfield_submittername" class="name_text"></fieldset><fieldset><label for="file_customfield_image"><strong>Image</strong></label><input id="file_customfield_image" name="file_customfield_image" class="fi" type="file"></fieldset><input type="hidden" name="type_customfield_image" value="image" /><input type="hidden" name="__mode" value="post" /><input type="hidden" name="blog_id" value="21" /><input type="hidden" id="magic_token" name="magic_token" value="" /><input type="hidden" id="entry-title" name="title" value="Web submission"/><input accesskey="s" name="uploadgo" id="comment-post" value="Upload Image" type="submit"></form>';
	$('#uploadformd').empty().html(uploadformz);

	//fallback remove if old code
	if($('#pageflip a span').length>0){ $('#pageflip').remove(); }
	//reorder elements from ad-server
	if($('#pageflip').length>0){
		var rplc01 = $('#pageflip a').attr('href');
		var rplc02 = $('#pageflip a img').attr('src');
		var rplc03 = $('#pageflip a img').attr('title');
		var rplc04 = '<a href="'+ rplc01 +'"><span class="dogear"><img src="http://guyswithiphones.com/code/page_flip.png" /></span><span class="msg"><img src="'+ rplc02 +'" alt="'+ rplc03 +'" title="'+ rplc03 +'" /></span></a>';
		$('#pageflip a').replaceWith(rplc04);
	}

	//ImageGalleryToggle
	$('#makesmall').click(function(){ $('#maincontent ul').attr({'class':'smalllist'}); $('#makebig').attr({'class':''}).css({'background-color':''}); $(this).attr({'class':'active'}); resizetheimages(); return false; });
	$('#makebig').click(function(){ $('#maincontent ul').attr({'class':'biglist'}); $('#makesmall').attr({'class':''}).css({'background-color':''}); $(this).attr({'class':'active'}); resizetheimages(); return false; });
	
	//MenuAnimation and ImageGalleryHover
	if(isIE){
		$('.menu li a').hover( function(){ $(this).animate({ backgroundColor: '#FF3600' }, 400).animate({ backgroundColor: '#FFD800', color: '#222222' }, 400); }, function(){ var currentclass = $(this).attr('class'); var col1 = '#555555'; var col2 = '#FFFFFF'; if(currentclass=='active'){ col1 = '#FFD800'; var col2 = '#222222'; }; $(this).animate({ backgroundColor: col1, color: col2 }, 400); } );
	}	
	$('#maincontent ul li a').hover( function(){ $(this).children(".imginfo").fadeOut(300); }, function(){ $(this).children('.imginfo').fadeIn(300); } );
	
	//DetailPage
	var bidivwidth = $('#bigimg').width();
	var biiwidth = $('#bigimg img').width();
	var biiheight = $('#bigimg').height();
	var bisze = '<span>' + biiwidth + ' &times; ' + biiheight + ' px';
	if(biiwidth>bidivwidth){
		var newheightBI = biiheight * ( bidivwidth / biiwidth ); newheightBI = Math.ceil(newheightBI);
		$('#bigimg img').css({'width':bidivwidth,'height':newheightBI});
		var bisze = bisze + ' (resized to fit)';
	}
	var bisze = bisze + '</span>';
	$('#idetsize').append(bisze);
	
	//validate form on keyup and submit
	$("#upform").validate({
		rules: { customfield_submittername: "required", customfield_submitteremail: {required: true, email: true}, file_customfield_image: "required" },
		messages: { customfield_submittername: "Please enter your name", customfield_submitteremail: "Please enter a valid email address", file_customfield_image: "Please select a file" }
	});
	
	$("#pageflip").hover(function(){
		$("#pageflip, #pageflip .dogear img").stop().animate({width:'307px',height:'319px'},500);
	},function(){
		$("#pageflip").stop().animate({width:'100px',height:'100px'},220);
		$("#pageflip .dogear img").stop().animate({width:'100px',height:'104px'},220);
	});
		
});
