/****************************************************************\
 *  This file Coded by : Al-Mothafar M. Al-Hasan
 *  http://www.almothafar.com
 *  Copyrighted for Digital Series Company - Jordan
 *  Copyrighted for Total Technology Solutions (Hulool) - KSA
 *  http://www.digital-series.com & http://www.hulool.com
 *  2010 (c)
 \***************************************************************/

$(function() {
    $('#gallery a').lightBox();
});

$(document).ready(function() {
    $(".moreFLinks").click(function(){
        if (!$(this).parent().children().is(':animated'))
        {
            $("#" + this.id + "-content").show(500, function() {}
                );
            $("#" + this.id).hide(500, function() {}
                );
        }
        return false;
    });
    $(".lessFLinks").click(function(){
        if (!$(this).parent().children().is(':animated'))
        {
            $("#" + this.id + "-content").hide(500, function() {}
                );
            $("#" + this.id).show(500, function() {}
                );
        }
        return false;
    });
});

function showLogin(id,show)
{
    var prop = "none";
    if (show.toString() == "true")
        prop = "inline";
    //var temp = document.getElementById(id).innerHTML;
    /*if (temp.toString().indexOf("You are signed in") != -1)
            return;*/

    var tempStr = window.location.href; // this to check if I'm in maximized mode (just login box showed in page)'
    if (tempStr.toString().indexOf("struts_action=%2Flogin") == -1 || tempStr.toString().indexOf("p_p_state=maximized") == -1)
        document.getElementById(id).style.display = prop;
}

function reduceText(str,max_characters)
{
    var strValue = str.toString();
    if (strValue.toString().length > max_characters)
        strValue = strValue.toString().substr(0, max_characters);
    //alert(strValue.toString());
    document.write(strValue);
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

var intervalT = null;
var counter = 0;
function showChildren()
{
    clearInterval(intervalT);
    counter = 0;
    //    document.getElementById("MySettings_Children").setAttribute("class", "hide_MySettings_Children");
    document.getElementById("MySettings_Children").className = "hide_MySettings_Children";
    //    document.getElementById("myBU_Children").setAttribute("class", "show_MyBU_Children");
    document.getElementById("myBU_Children").className = "show_MyBU_Children";
}


function hideChildren()
{
    clearInterval(intervalT);
    intervalT = setInterval(hideChildren, 5000);
    if (counter == 0 )
    {
        counter++;
        return;
    }
    //    document.getElementById("myBU_Children").setAttribute("class", "hide_MyBU_Children");
    document.getElementById("myBU_Children").className = "hide_MyBU_Children";
}



var intervalT1 = null;
var counter1 = 0;
function showChildrenSettings()
{
    clearInterval(intervalT1);
    counter1 = 0;
    //    document.getElementById("MySettings_Children").setAttribute("class", "show_MySettings_Children");
    document.getElementById("MySettings_Children").className = "show_MySettings_Children";
    //    document.getElementById("myBU_Children").setAttribute("class", "hide_MyBU_Children");
    document.getElementById("myBU_Children").className = "hide_MyBU_Children";
}

function hideChildrenSettings()
{
    clearInterval(intervalT1);
    intervalT1 = setInterval(hideChildrenSettings, 5000);
    if (counter1 == 0 )
    {
        counter1++;
        return;
    }
    //    document.getElementById("MySettings_Children").setAttribute("class", "hide_MySettings_Children");
    document.getElementById("MySettings_Children").className = "hide_MySettings_Children";
}

var vimeoThumbnail;
function getVimeoThumbnail(clips)
{
    for (var i = 0; i < clips.length; i++) {
        var teste = clips[i].thumbnail_medium
        document.getElementById("thumbs").innerHTML='';
        //thumbs.innerHTML = '';
        var thumb = document.createElement('img');
        thumb.setAttribute('src', clips[i].thumbnail_medium);
        thumb.setAttribute('alt', clips[i].title);
        thumb.setAttribute('title', clips[i].title);

        vimeoThumbnail = thumb.getAttribute('src');
//        document.getElementById("thumbs").appendChild(thumb);
    }
}

function processVimeoThumbnail(id)
{
    var js = document.createElement('script');
    var url = 'http://vimeo.com/api/clip/'+id+'.json?callback=getVimeoThumbnail'
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', url);
    document.getElementsByTagName('head').item(0).appendChild(js);
}

//function htmlVideoBox(url,width,height,title,autoplay,isDirectLink,imgSrc)
function htmlVideoBox(url,title,autoplay,imgSrc)
{
//    var vWidth = width;
//    var vHeight = height;
//    var vIsDirect = false;
    var vAutoplay = 0;
    var vThumbImg = "";
    var htmlCode = "";
    var videoID = "";
    var firstIndex = -1;
    var secondIndex = -1;
    
    if (autoplay == true || autoplay.toString() == '1')
        vAutoplay = 1;

//    if (isDirectLink == true || isDirectLink.toString() == '1')
//        vIsDirect = true;

    if (imgSrc == '' || imgSrc.toString() == 'undefined' || imgSrc == null || imgSrc.toString().length < 10)
        vThumbImg = "/Al-Baha/albaha/images/logoAr.jpg";
    else
        vThumbImg = imgSrc;

//    if (vIsDirect != true)
//    {
        if (url.toString().indexOf("youtube.com") >= 0)
        {
            firstIndex = url.toString().indexOf("watch?v=") + 8;
            secondIndex = url.toString().indexOf("&", firstIndex);

            if (secondIndex < 1)
                videoID = url.toString().substring(firstIndex).toString();
            else
                videoID = url.toString().substring(firstIndex,secondIndex).toString();

            if (imgSrc == '' || imgSrc.toString() == 'undefined' || imgSrc == null || imgSrc.toString().length < 10)
                vThumbImg = "http://i.ytimg.com/vi/"+videoID+"/1.jpg";

//            var htmlCode = "";
//            htmlCode += '<object width="'+vWidth+'" height="'+vHeight+'">';
//            htmlCode += '<param name="movie" value="http://www.youtube.com/v/'+videoID+'"></param>';
//            htmlCode += '<param name="allowFullScreen" value="true"></param>';
//            htmlCode += '<param name="allowscriptaccess" value="always"></param>';
//            htmlCode += '<embed src="http://www.youtube.com/v/'+videoID+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+vWidth+'" height="'+vHeight+'">';;
//            htmlCode += '</object>';

            htmlCode = "";
            htmlCode += '<a rel="#voverlay" href="http://www.youtube.com/v/'+videoID+'?autoplay'+vAutoplay+'&rel=0" title="' + title + '">';
            htmlCode += '<img src="'+ vThumbImg + '" alt="' + title + '" />';
            htmlCode += '<span>'+ title +'</span></a>';

            return htmlCode;
        }
//    }
        else if (url.toString().indexOf("vimeo.com") >= 0)
        {
            firstIndex = url.toString().indexOf("vimeo.com/") + 10;
            secondIndex = url.toString().indexOf("?", firstIndex);

            if (secondIndex < 1)
                videoID = url.toString().substring(firstIndex).toString();
            else
                videoID = url.toString().substring(firstIndex,secondIndex).toString();

            if (imgSrc == '' || imgSrc.toString() == 'undefined' || imgSrc == null || imgSrc.toString().length < 10) // If true let start getting vimeo thumbnail war !!!!
            {
                processVimeoThumbnail(videoID);
                vThumbImg = vimeoThumbnail;
            }

            htmlCode = "";
            htmlCode +='<a title="'+title+'" href="http://vimeo.com/moogaloop.swf?clip_id='+videoID+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;autoplay='+vAutoplay+'" rel="#voverlay">';
            htmlCode +='<img alt="'+title+'" src="'+vThumbImg+'">';
            htmlCode +='</a>' ;

            return htmlCode;
        }
        else
        {
            htmlCode += '<a href="/Al-Baha_Inner/albaha-in/javascript/engine/swf/player.swf?url=' + url + '" title="' + title + '" rel="#voverlay">';
            htmlCode += '<img src="'+ vThumbImg + '" alt="' + title + '" style="height: 120px;" />';
            htmlCode += '<span>'+ title +'</span></a>';

            return htmlCode;
        }

    return "<span>Sorry, there is an error with URL, maybe you have a mistake or video provider not supported.</span>";
}
