/*
 * Function for replacing text inside a text input field.
 * Mainly used for 'blanking' out the field onClick
 *
 */

function inputReplaceValue(element, checkfor, replacewith) {
    if (element.value == checkfor) {
        element.value = replacewith;
    }
}

function toggleVisible(element) {
    var target = document.getElementById(element);

    if (target.style.display == 'none') {
        target.style.display = 'block';
    } else {
        target.style.display = 'none';
    }
}

function toggleImage(element, image1, image2) {
    image1 = "http://" + window.location.hostname + image1;
    image2 = "http://" + window.location.hostname + image2;

    if (element.src == image1) {
        element.src = image2;
    } else {
        element.src = image1;
    }
}

// creates a confirmation Dialog for a checkbox
function msgBox(text, obj)
 {
    var box = window.confirm(text);
    if (box == true) {
        } else if (box == false) {
        obj.checked = false;
    }
}

function insert(aTag, eTag, form, textarea) {
    var input = document.forms[form].elements[textarea];
    input.focus();

    /* for Internet Explorer */
    if (typeof document.selection != 'undefined') {
        /* put in the format code */
        var range = document.selection.createRange();
        var insText = range.text;
        range.text = aTag + insText + eTag;
        /* setting up the cursor position */
        range = document.selection.createRange();
        if (insText.length == 0) {
            range.move('character', -eTag.length);
        } else {
            range.moveStart('character', aTag.length + insText.length + eTag.length);
        }
        range.select();
    }
    /* for Gecko based browser */
    else if (typeof input.selectionStart != 'undefined')
    {
        /* put in the format code */
        var start = input.selectionStart;
        var end = input.selectionEnd;
        var insText = input.value.substring(start, end);
        input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
        /* setting up the cursor position */
        var pos;
        if (insText.length == 0) {
            pos = start + aTag.length;
        } else {
            pos = start + aTag.length + insText.length + eTag.length;
        }
        input.selectionStart = pos;
        input.selectionEnd = pos;
    }
    /* for the rest of the browserworld */
    else
    {
        var pos;
        var re = new RegExp('^[0-9]{0,3}$');
        while (!re.test(pos)) {
            pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
        }
        if (pos > input.value.length) {
            pos = input.value.length;
        }
        var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
        input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
    }
}

function setUpSmilies(form, textarea) {

    var smilies = document.getElementById('smileBox');
    var nodes = smilies.childNodes;
    for (var ii in nodes) {
        el = nodes[ii];
        el.onclick = function() {
            insert(this.title, '', form, textarea);
        };
    }
}



(function ($J) {

  $J(function () {

  var deleteForm = $J('form#delete-profile');
  var deleteLink = $J('a#delete-confirm', deleteForm);
  if(deleteLink.length) {
    deleteLink.bind('click', function (e) {
      e.preventDefault();
      if(confirm('Do you want Grownups.co.nz to delete your profile?')) {
        deleteForm.submit();
      } else {
        // Something else here?
      }
    });
  }
  
	$J('#gallery').each(function(){
		var nbElmt = $J(this).children('.gallery-item').size();
		$J(this).find('.gallery-item').hide();
		$J(this).find('.gallery-item').eq(0).attr('id', 'current').show();
		
		
		$J(this).append('<p class="pagination"><a href="#" class="prev" title="See previous article">&laquo;</a><span>1 / '+nbElmt+'</span><a href="#" class="next" title="See next article">&raquo;</a></p>');	
		$J(this).find('.gallery-item h2').css({'margin-top': '5px'});
												
		function prevItem() {
			var index = $J('.gallery-item').index($J('#gallery #current'));
			if (index == 0) {
				var newIndex = nbElmt - 1;
			} else {
				var newIndex = index - 1;
			}
			$J('#gallery .gallery-item').eq(index).removeAttr('id').fadeOut('slow', function() {
				$J('#gallery .gallery-item').eq(newIndex).attr('id','current').fadeIn();
			
			});		
			$J('#gallery .pagination span').html((newIndex + 1) + ' / ' +nbElmt);
		}
		
		function nextItem() {
			var index = $J('.gallery-item').index($J('#gallery #current'));
			if (index == (nbElmt - 1)) {
				var newIndex = 0;
			} else {
				var newIndex = index + 1;
			}
			$J('#gallery .gallery-item').eq(index).removeAttr('id').fadeOut('slow', function() {
				$J('#gallery .gallery-item').eq(newIndex).attr('id','current').fadeIn();
			
			});	
			$J('#gallery .pagination span').html((newIndex + 1) + ' / ' +nbElmt);
		}
		
		$J(this).find('.prev').click(function() {
			prevItem();	
			return false;			
		});
		$J(this).find('.next').click(function() {
			nextItem();
			return false;				
		});
		
		setInterval(nextItem,6000); 
		/*
		$J(this).find('#current img').mousemove(function(e){
			var x = e.pageX - $J(this).offset().left;			if (x <= 80) {
				$J(this).parents('#gallery').find('.prev').show();
			} else if (x >= 255) {
				$J(this).parents('#gallery').find('.next').show();
			} else {
				$J(this).parents('#gallery').find('.prev').hide();
				$J(this).parents('#gallery').find('.next').hide();
			}
		});
		*/
		
	});
  
  

    /*
     * Track links in Google Analytics
     */
    // Site wide notices
    $J('.notification a').bind('click', function (event) {
      // OLD GA Code
      //pageTracker._trackEvent('Notifications', $J(this).text());

      _gaq.push(['_trackEvent', 'Notifications', $J(this).text()]);
    });

    // Homepage slider
    $J('.gallery-item a').bind('click', function (event) {
      var slider = $J(this).parents('.gallery-item');
      var sliderTitle = $J('h2 a', slider).text();

      _gaq.push(['_trackEvent', 'Sliders', sliderTitle]);
    });

    $J("#area_id0").change();

    $J(".adzone").each(function() {
        var showthis = true;

        $J(this).find("a img").each(function() {
            if ($J(this).width() == 1) { // empty placeholder ad - nuke it
                showthis = false;
            }
        });

        if (showthis)
            $J(this).css({ "margin-top": "0", "position": "relative" }); // this is awful, but browsers wont read .width() if the element is hidden.
        else
            $J(this).hide();
    });

    $J("#menu_1>li").hoverIntent({
        over: function() {
            var thisdropdown = $J(this).children(".nav_dropdown");

            $J(".nav_dropdown:visible").removeShadow().hide();
            $J(thisdropdown).show();

            var this_offset = $J(thisdropdown).offset();
            var this_width  = $J(thisdropdown).outerWidth();
            var pos_r       = this_offset.left + this_width;
            var pos_fix     = $J(window).width() - pos_r - 30;

            if (pos_r > $J(window).width())
                $J(thisdropdown).css("left", pos_fix);

            $J(thisdropdown).dropShadow();
        },
        out: function() {
            $J(this).children(".nav_dropdown:visible").removeShadow().hide();
        },
        timeout: 0
    });
    
  });

})(jQuery);


