
// JavaScript Document

jQuery.noConflict();
hs.showCredits = false;

hs.graphicsDir = 'highslide/graphics/';
hs.outlineType = null;
hs.skin.contentWrapper =
'<div class="highslide-body"></div>';


function showMediumProfile(obj, id)
{   
    hs.skin.contentWrapper =
    '<div id="m_h_body" class="highslide-body2" style="border:0px solid pink; margin-left:37px; margin-top:70px;"></div>';    
    jQuery.ajax({
            type: "POST",
            url: obj.href,
//            data: 'name=' + jQuery(this).attr('rel'),
            dataType: "html",

            success: function(data){                          
                jQuery("#m_highslide_maincontent_" + id).html(data);
                hs.htmlExpand(obj);
                jQuery("#m_highslide_maincontent_" + id).html("");
                //Scroller.init(id, 270);
				//jQuery(document).ready(function() {
				//	jQuery("div.highslide-body2").corner("round");
				//});
            },
            error: function(){
                alert('Error');
            }
        });
    return false;
}



function showPopBox_lou(obj, oid)
{    
	obj.href = addRandom(obj.href);
    hs.close();
    hs.skin.contentWrapper = '<div class="highslide-body"></div>';   

    jQuery.ajax({
        type: "GET",
        url: obj.href +'&template=profile_shadowbox',
        dataType: "html",
        success: function(data){
			
			data = addScrollerLink(data, oid);
            jQuery("#highslide_maincontent_" + oid).html(data);
			hs.lou_Expand(obj, {width: 490, height: 700},data);
			hs.fullExpandPosition = 'bottom left';
            jQuery("#highslide_maincontent_" + oid).html("");            
            Scroller.init(oid, 270);
        },
        error: function(){
            alert('Error');
        }
    });
    return false;
}


function showPopBox(obj, oid)
{    
	obj.href = addRandom(obj.href);

    hs.close();    
    hs.skin.contentWrapper = '<div class="highslide-body"></div>';   

    jQuery.ajax({
        type: "GET",
        url: obj.href +'&template=profile_shadowbox',
        dataType: "html",
        success: function(data){  
			data = addScrollerLink(data, oid);
            jQuery("#highslide_maincontent_" + oid).html(data);
			hs.htmlExpand(obj, {width: 490, height: 700});
			hs.fullExpandPosition = 'bottom left';
            jQuery("#highslide_maincontent_" + oid).html("");            
            Scroller.init(oid, 270);
			
        },
        error: function(){
            alert('Error');
        }
    });
    return false;
}

function showPopBoxp(obj, oid)
{    
	obj.href = addRandom(obj.href);
    hs.close();    
    hs.skin.contentWrapper = '<div class="highslide-body"></div>';   
    jQuery.ajax({
        type: "POST",
        url: obj.href + '&template=profile_shadowbox',
        dataType: "html",

        success: function(data){  
            jQuery("#p_highslide_maincontent_" + oid).html(data);			
			hs.htmlExpand(obj, {width: 490, height: 700});
			hs.fullExpandPosition = 'bottom left';
            jQuery("#p_highslide_maincontent_" + oid).html("");            
            Scroller.init(oid, 270);
        },
        error: function(){
            alert('Error');
        }
    });
    return false;
}

function showCommentBox_lou(obj, id, t)
{
    hs.close();  
	//alert("a");
	//hs.skin.contentWrapper = '<div class="highslide-body"></div>';   
    jQuery.ajax({
        type: "POST",
        url: obj.href + '&template=profile_shadowbox',
        data: 'name=' + jQuery(this).attr('rel'),
        dataType: "html",

        success: function(data){
            jQuery("#profile_comment_" + id).html(data);
            hs.htmlExpand(obj, {width: 490});
            jQuery("#profile_comment_" + id).html("");
            Scroller.init( id + '' + t + "_0", 230);
        },
        error: function(){
            alert('Error');
        }
    });              
    return false;
}


function onSubmitComment_lou(url, id)
{
    jQuery.ajax({
        type: "POST",
        //url: url + "/index.php?option=com_profile&view=fullprofile",
        url: url,
        data: jQuery('#commentform_'+id).serialize() + "&getlistajax=1&sign=" + Math.random(), 
        dataType: "html",

        success: function(data){
            jQuery('#comments').val("");
            alert("Comments submit successfully.");                
            jQuery('#scr_content' + id + "_0").html(data);
        },
        error: function(){
            alert('Error');
        }
    });
    return false;
}

function showCommentBox(obj, id)
{
    hs.close();  
	//alert("a");
	//hs.skin.contentWrapper = '<div class="highslide-body"></div>';   
    jQuery.ajax({
        type: "POST",
        url: obj.href + '&template=profile_shadowbox',
        data: 'name=' + jQuery(this).attr('rel'),
        dataType: "html",

        success: function(data){
            jQuery("#profile_comment_" + id).html(data);
            hs.htmlExpand(obj, {width: 490});
            jQuery("#profile_comment_" + id).html("");
            Scroller.init(id + "_0", 230);
        },
        error: function(){
            alert('Error');
        }
    });              
    return false;
}


function onSubmitComment(url, id)
{

	//alert(jQuery('#commentform').serialize() + "&getlistajax=1&sign=" + Math.random());
	
    jQuery.ajax({
        type: "POST",
        //url: url + "/index.php?option=com_profile&view=fullprofile",
        url: url,
        data: jQuery('#commentform').serialize() + "&getlistajax=1&sign=" + Math.random(), 
        dataType: "html",

        success: function(data){
            jQuery('#comments').val("");
            alert("Comments submit successfully.");                
            jQuery('#scr_content' + id + "_0").html(data);
        },
        error: function(){
            alert('Error');
        }
    });
    return false;
}


//plugin
function showPopBoxc(obj, cid)
{    
	//alert('cccc');
    hs.close();    
    hs.skin.contentWrapper = '<div class="highslide-body"></div>';   
    jQuery.ajax({
        type: "POST",
        url: obj.href + '&template=profile_shadowbox',
        dataType: "html",

        success: function(data){  
            jQuery("#c_highslide_maincontent_" + cid).html(data);			
			hs.htmlExpand(obj, {width: 490, height: 700});			
			hs.fullExpandPosition = 'bottom left';
            jQuery("#c_highslide_maincontent_" + cid).html("");            
            Scroller.init(oid, 270);
        },
        error: function(){
            alert('Error');
        }
    });
    return false;
}




// ============================================================================== popbox
/*
anchor:  the event ancher object.
divIdNo:  whitch div to show the data. ("highslide_maincontent_" + divIdNo)
*/

// show popbox of id tag
function showPopBox_idTag(anchor, divIdNo)
{
	anchor.href = addRandom(anchor.href);
	hs.close();
	hs.skin.contentWrapper = '<div class="highslide-body"></div>';

	jQuery.ajax({
		type: "GET",
		url: anchor.href +'&template=profile_shadowbox',
		dataType: "html",
		success: function(data){  
			data = addScrollerLink(data, divIdNo);
			jQuery("#highslide_maincontent_" + divIdNo).html(data);
			hs.htmlExpand(anchor, {width: 490, height: 700});
			//hs.lou_Expand(anchor, {width: 490, height: 700},data);
			hs.fullExpandPosition = 'bottom left';
			jQuery("#highslide_maincontent_" + divIdNo).html("");
			Scroller.init(divIdNo, 270);
			
		},
		error: function(){
			alert('Error');
		}
	});
	return false;
}

function showPopBox_profile(anchor, divIdNo)
{
	anchor.href = addRandom(anchor.href);
	hs.close();
	hs.skin.contentWrapper = '<div class="highslide-body"></div>';

	jQuery.ajax({
		type: "POST",
		url: anchor.href +'&template=profile_shadowbox',
		dataType: "html",
		success: function(data){
			data = addScrollerLink(data, divIdNo);
			jQuery("#highslide_maincontent_" + divIdNo).html(data);
			hs.lou_Expand(anchor, {width: 490, height: 700},data);
			hs.fullExpandPosition = 'bottom left';
			jQuery("#highslide_maincontent_" + divID).html("");
			Scroller.init(divIdNo, 270);
		},
		error: function(){
			alert('Error');
		}
	});
	return false;
}

function showPopBox_comment(anchor, divIdNo)
{
	hs.close();  
	jQuery.ajax({
		type: "POST",
		url: anchor.href + '&template=profile_shadowbox',
		data: 'name=' + jQuery(this).attr('rel'),
		dataType: "html",

		success: function(data){
			jQuery("#profile_comment_" + divIdNo).html(data);
			hs.htmlExpand(anchor, {width: 490});
			jQuery("#profile_comment_" + divIdNo).html("");
			Scroller.init( divIdNo + "_0", 230);
		},
		error: function(){
			alert('Error');
		}
	});
	return false;
}

function showPopBox_onSubmitComment(url, divIdNo)
{
	jQuery.ajax({
		type: "POST",
		url: url,
		data: jQuery('#commentform_'+divIdNo).serialize() + "&getlistajax=1&r=" + Math.random(),
		dataType: "html",

		success: function(data){
			jQuery('#comments').val("");
			alert("Comments submit successfully.");
			jQuery('#scr_content' + divIdNo + "_0").html(data);
		},
		error: function(){
			alert('Error');
		}
	 });
	return false;
}

function showPopBox_query(anchor, divIdNo)
{
	hs.close();
	jQuery.ajax({
		type: "POST",
		url: anchor.href + '&template=profile_shadowbox',
		data: 'name=' + jQuery(this).attr('rel'),
		dataType: "html",

		success: function(data){
			jQuery("#q_profile_comment_" + divIdNo).html(data);
			hs.htmlExpand(anchor, {width: 490});
			jQuery("#q_profile_comment_" + divIdNo).html("");
			Scroller.init(divIdNo + "_0", 230);
		},
		error: function(){
			alert('Error');
			}
	});
	return false;
}
// ============================================================================== popbox


/*
replace <a href="#name"> for the "3.20 Bug with HTML ANCHORS in Profile Component"
*/
function addScrollerLink(data, divIdNo)
{
	var r = Math.floor(Math.random()*100000);
	var re = new RegExp("<a href=\"#([^\"]+)\"", "ig");
	var str = data.replace(re, "<a href=\"javascript:scrollerJumpTo('$1_"+r+"', '"+divIdNo+"');\"");
	re = new RegExp("<a name=\"([^\"]+)\"", "ig");
	str = str.replace(re, "<a name=\"$1_"+r+"\"");
	return str;
}

// scroller jump.
function scrollerJumpTo(ancherName, divIdNo)
{
	var originY = 0;
	var targetY = 0;

	var scrollerOrigin = document.getElementById("scr_content"+divIdNo);
	var link = document.getElementsByTagName("a");

	originY = getTop(scrollerOrigin);

	for(var i = link.length-1; i>=0; i--)
	{
		if(link[i].name == ancherName)
		{
			targetY = getTop(link[i]);
			break;
		}
	}

	var scrollY = targetY-originY;

	Scroller.jumpTo(scrollY);
}

// get element absolute top position. 
function getTop(e){
	var top=e.offsetTop;
	while(e=e.offsetParent){
		top+=e.offsetTop;
	}
	return top;
}

// add a random numeber param to the url
function addRandom(url)
{
	var str = url;
	if(str.indexOf("?")==-1)
	{
		str += "?rand=" + Math.random();
	}
	else
	{
		if(str.indexOf("rand")==-1)
		{
			str += "&rand=" + Math.random();
		}
		else
		{
			var arr = str.split("&");
			while(arr[arr.length-1].indexOf("rand")>=0)
			{
				arr.pop();
			}
			str = arr.join("&");
			str += "&rand=" + Math.random();
		}
	}
	return str
}
