$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
    $(pager).find('li').removeClass('currentli')
        .filter('li:eq('+currSlideIndex+')').addClass('currentli');
};

// single property
$(document).ready(function() {
    $('#imageframe').cycle({
        timeout: 0,
        pager:  '#imagethumbs',
		next: '.next',
		prev: '.prev',
        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#"><img src="' + slide.src + '" width="120" height="81" /></a></li>';
        }
    });
});

function simulateMouseOver(elem) {
	if( document.createEvent ) {
		var evObj = document.createEvent('MouseEvents');
		evObj.initEvent( 'mouseover', true, false );
		elem.dispatchEvent(evObj);
	} else if( document.createEventObject ) {
		elem.fireEvent('onmouseover');
	}
}

function unSimulateMouseOver(elem) {
	if( document.createEvent ) {
		var evObj = document.createEvent('MouseEvents');
		evObj.initEvent( 'mouseout', true, false );
		elem.dispatchEvent(evObj);
	} else if( document.createEventObject ) {
		elem.fireEvent('onmouseout');
	}
}

function getPrevSlide(num, top) {
	var prevnum = (num-1);
	if (prevnum < 1) {
		return top;
	} else {
		return prevnum;
	}
}

function getNextSlide(num, top) {
	var nextnum = (num+1);
	if (nextnum > top) {
		return 1;
	} else {
		return nextnum;
	}
}

function updateProperty(id) {
	var address = $('#hpimg_1 a:nth-child('+id+')').metadata({type: 'attr', name: 'data'}).address;
	var price = $('#hpimg_1 a:nth-child('+id+')').metadata({type: 'attr', name: 'data'}).price;
	var href = $('#hpimg_1 a:nth-child('+id+')').attr("href");
	$(".info_hpimg").parent().attr("href", href);
	$(".info_hpimg").html("<p><strong>Address: </strong>"+address+"</p><p><strong></strong> "+price+"</p>");
}


function getCurIndex (whichz) {
	imglen = $("#hpimg_1 a").size();
	i=0;

	while (i < imglen) {
		var nchild = (i+1);
		var currimg = parseInt($("#hpimg_1 a:nth-child("+nchild+")").css("z-index"));
		if (currimg == whichz) {
			return (i+1);
		} else {
			i++;
		}
	}
}

function checkCurrentStatus() {
	// count number of images
	imglen = $("#hpimg_1 a").size();
	i=0;
	var zin = new Array();
	while (i < imglen) {
		var nchild = (i+1);
		zin[i] = parseInt($("#hpimg_1 a:nth-child("+nchild+")").css("z-index"));
		i++;
	}
	var max_zindex = Math.max.apply(null, zin);
	var output = getCurIndex(max_zindex);
	return output;
}


$(document).ready(function() {
	$(".prev").click(function() {
		var currentid = checkCurrentStatus();
		var top = $("#hpimg_1 a").size();
		var goIndex = getPrevSlide(currentid, top);
		updateProperty((goIndex));
	});
	
	$(".next").click(function() {
		var currentid = checkCurrentStatus();
		var top = $("#hpimg_1 a").size();
		var goIndex = getNextSlide(currentid, top);
		updateProperty((goIndex));
	});
	
	$("#imagescroller").mouseenter(function() {
		$(".prev").fadeIn("fast");
		$(".next").fadeIn("fast");
	}).mouseleave(function() {
		$(".prev").fadeOut();
		$(".next").fadeOut();
	});
	
	// front page
	$('#imageparent').mouseenter(function() {
		//var currentid = $("#hpimg_1_parent img.currentimg").attr("id");
		//console.log($("#hpimg_1_parent").html());
		//var nchild = parseInt(currentid);
		//console.log('#hpimg_1 img:nth-child('+currentid+')')
		/*$('#hpimg_1').cycle('pause');
		$('#hpimg_2').cycle('pause');		
		$('#hpimg_3').cycle('pause');*/
		var currentid = checkCurrentStatus();
		updateProperty(currentid);
		$(".info_hpimg").show();
		$(".prev").fadeIn("fast");
		$(".next").fadeIn("fast");
	}).mouseleave(function() {
		/*$('#hpimg_1').cycle('resume');
		$('#hpimg_2').cycle('resume');		
		$('#hpimg_3').cycle('resume');*/
		$(".info_hpimg").hide();
		$(".prev").fadeOut();
		$(".next").fadeOut();
	});

    $('#hpimg_1').cycle({timeout: 6000, pause: 1, next: ".next", prev: ".prev"});	
	
	/*$('#hpimg_1_parent').mouseenter(function() {
		$("#hpimg_1_parent .info_hpimg").fadeIn();
		document.getElementById
		//simulateMouseOver(parent);
		}).mouseleave(function() {$("#hpimg_1_parent .info_hpimg").fadeOut();});
	$('#hpimg_2_parent').mouseenter(function() {$("#hpimg_2_parent .info_hpimg").fadeIn();}).mouseleave(function() {$("#hpimg_2_parent .info_hpimg").fadeOut();});
	$('#hpimg_3_parent').mouseenter(function() {$("#hpimg_3_parent .info_hpimg").fadeIn();}).mouseleave(function() {$("#hpimg_3_parent .info_hpimg").fadeOut();});*/
/*fadeBox(1);
fadeBox(2);
fadeBox(3);*/
});

function stripHTML(oldString) {

   var newString = "";
   var inTag = false;
   for(var i = 0; i < oldString.length; i++) {
   
        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
              if(oldString.charAt(i+1)=="<")
              {
              		//dont do anything
	}
	else
	{
		inTag = false;
		i++;
	}
        }
   
        if(!inTag) newString += oldString.charAt(i);

   }

   return newString;
}

$(document).ready(function() {
	var link = window.location.href;
	var title = "Anthony Diaz-Perez: " + escape($("#text h1").html());
	$("#sociallinks a.social_fb").attr("href", "http://www.facebook.com/sharer.php?u="+link+"&t="+title);
	$("#sociallinks a.social_tw").attr("href", "http://twitter.com/share?url="+link);
	//$("#images").fadeIn("slow");
});
