/* Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 2.1
 * 
 * Thanks to 
 * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 * Tom Leonard for some improvements
 * 
 */
jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $('#imgLink').getUrlParam("paramName");
*/ 
 getUrlParam: function(strParamName){
	  strParamName = escape(unescape(strParamName));
	  
	  var returnVal = new Array();
	  var qString = null;
	  
	  if ($(this).attr("nodeName")=="#document") {
	  	//document-handler
		
		if (window.location.search.search(strParamName) > -1 ){
			
			qString = window.location.search.substr(1,window.location.search.length).split("&");
		}
			
	  } else if ($(this).attr("src")!="undefined") {
	  	
	  	var strHref = $(this).attr("src")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else if ($(this).attr("href")!="undefined") {
	  	
	  	var strHref = $(this).attr("href")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else {
	  	return null;
	  }
	  	
	  
	  if (qString==null) return null;
	  
	  
	  for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
			
	  }
	  
	  
	  if (returnVal.length==0) return null;
	  else if (returnVal.length==1) return returnVal[0];
	  else return returnVal;
	}
});
function urldecode( str ) {
    // Decodes URL-encoded string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_urldecode/
    // +       version: 804.1715
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    
    var ret = str;
       
    ret = ret.replace(/\+/g, '%20');
    ret = decodeURIComponent(ret);
    ret = ret.toString();

    return ret;
}
function advancedSearch(){
	jQuery(document).ready(function($){
		/**
		 * Animates the Legend WHAT to collapse and expand
		 * Uses Cookies to remember the previous state when navigating away
		 */
		if ($.cookies.get('what') == 'true') {
			$('#what').removeClass('expand_fieldset').addClass('collapse_fieldset');
		}
		else {
			$('#what').addClass('collapse_fieldset').parent().children().filter("p,img,table,ul,div,span").show();
			$.cookies.set('what', 'true');
		}
		$('#what').click(function(){
			if ($.cookies.get('what') == 'false') {
				$(this).removeClass('expand_fieldset').addClass('collapse_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('what', 'true');
			}
			else {
				$(this).removeClass('collapse_fieldset').addClass('expand_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('what', 'false');
			}
			return false;
		});
		/**
		 * Animates the Legend WHERE to collapse and expand
		 * Uses Cookies to remember the previous state when navigating away
		 */
		if ($.cookies.get('where') == 'true') {
			$('#where').removeClass('expand_fieldset').addClass('collapse_fieldset');
		}
		else {
			$('#where').addClass('expand_fieldset').parent().children().filter("p,img,table,ul,div,span").hide();
			$.cookies.set('where', 'false');
		}
		$('#where').click(function(){
			if ($.cookies.get('where') == 'false') {
				$(this).removeClass('expand_fieldset').addClass('collapse_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('where', 'true');
			}
			else {
				$(this).removeClass('collapse_fieldset').addClass('expand_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('where', 'false');
			}
			return false;
		});
		/**
		 * Animates the Legend COLLECTIONS to collapse and expand
		 * Uses Cookies to remember the previous state when navigating away
		 */
		if ($.cookies.get('collections') == 'true') {
			$('#collections').removeClass('expand_fieldset').addClass('collapse_fieldset');
		}
		else {
			$('#collections').addClass('expand_fieldset').parent().children().filter("p,img,table,ul,div,span").hide();
			$.cookies.set('collections', 'false');
		}
		$('#collections').click(function(){
			if ($.cookies.get('collections') == 'false') {
				$(this).removeClass('expand_fieldset').addClass('collapse_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('collections', 'true');
			}
			else {
				$(this).removeClass('collapse_fieldset').addClass('expand_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('collections', 'false');
			}
			return false;
		});
		/**
		 * Animates the Legend REFERENCES to collapse and expand
		 * Uses Cookies to remember the previous state when navigating away
		 */
		if ($.cookies.get('references') == 'true') {
			$('#references').removeClass('expand_fieldset').addClass('collapse_fieldset');
		}
		else {
			$('#references').addClass('expand_fieldset').parent().children().filter("p,img,table,ul,div,span").hide();
			$.cookies.set('references', 'false');
		}
		$('#references').click(function(){
			if ($.cookies.get('references') == 'false') {
				$(this).removeClass('expand_fieldset').addClass('collapse_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('references', 'true');
			}
			else {
				$(this).removeClass('collapse_fieldset').addClass('expand_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('references', 'false');
			}
			return false;
		});
		/**
		 * Animates the Legend OTHER to collapse and expand
		 * Uses Cookies to remember the previous state when navigating away
		 */
		if ($.cookies.get('other') == 'true') {
			$('#other').removeClass('expand_fieldset').addClass('collapse_fieldset');
		}
		else {
			$('#other').addClass('collapse_fieldset').parent().children().filter("p,img,table,ul,div,span").show();
			$.cookies.set('other', 'true');
		}
		$('#other').click(function(){
			if ($.cookies.get('other') == 'false') {
				$(this).removeClass('expand_fieldset').addClass('collapse_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('other', 'true');
			}
			else {
				$(this).removeClass('collapse_fieldset').addClass('expand_fieldset').parent().children().filter("p,img,table,ul,div,span").toggle('fast');
				$.cookies.set('other', 'false');
			}
			return false;
		});
	});
}
$(document).ready(function(){
						   
	// focus on the first text input field in the first field on the page
    //$("input[type='text']:first", document.forms[0]).focus();
		$("input#field_text").click(function() {
			
			var value = $(this).attr("value");
			
			if (value ==='Type Here') {
				$(this).attr("value","");
			}
			
	});
	
	//link to external pages. e.g. terms and conditions
	$("form#register label a.external, div#navigation ul li a.external").click(function() {
													   
		$(this).attr('target','_blank');
		
	});
	

	//validation
	//form#advanced, 
	$("form#mycanmore, form#login, form#register, form#forgot, form#settings, form#flickr_upload, form#flickr_update, form#contact_user").validate({
   		submitHandler: function(form) {
       		//$('.loading').prev('form').hide();
			$('.loading').css('visibility','visible');
			//alert('Thanks!')
   			$(form).submit();
   		}
	});	
	
	//lightbox effects
	$('a[@class*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel	
	
	//tooltips
   $("form#advanced input[class!='submit'], form#mycanmore input[class!='submit'],select,form#flickr_upload input[class!='submit'], form#flickr_tags input[class!='submit'], form#flickr_update input[class!='submit'],textarea,form#settings input[class!='submit'], ,form#register input[class!='submit'],form#login input[class!='submit'],form#contact_user input[class!='submit']").focus(function() { //[id!='site_country'][id!='image_only_chk']
   		var input_title = $(this).parent().attr('title');
		$(this).parent().before('<p class="tooltip">'+input_title+'</p>');
		//$(this).css('background-color','lightyellow');
		$('.tip').css('visibility','hidden');
		//$(this).parent('p').addClass('hasFocus');
   }).blur(function() {
   		$('.tooltip').remove();
		//$(this).css('background-color','');
		//$(this).parent('p').removeClass('hasFocus');
   });
   
   // Site Scope Notes
   $(".infoTip").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 0,
		fixPNG: true,
		showBody: " - ",
		extraClass: "infoTip",
		top: 15,
		left: 15
	});

   
   // COOKIES //
   //$('#sitename').cookieBind();
   /**
    * deletes all cookies when a new search is performed
    */
   $('.new_search').click(function(){
   		$.cookies.del('what');
		$.cookies.del('where');
		$.cookies.del('collections');
		$.cookies.del('references');
		$.cookies.del('other');
	});
   /*$('#image_upload_submit').click(function(){
		if ("form#flickr_upload label:has('error')"){
			
		} else {
			$(this).parent().after('<img src="/images/loading.gif" alt="loading gif" id="image_upload_loading" />');
		}
	});*/
});
