// Copyright 2010 htmldrive.net Inc.
/**
 * @projectHomepage: http://www.htmldrive.net/go/to/desSlideshow
 * @projectDescription: Stylish featured image slideshow jQuery plugin.
 * @author htmldrive.net
 * More script and css style : htmldrive.net
 * @version 1.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */
     var j = jQuery.noConflict();
     
    showpic = function(){
    
        clearTimeout(v);
        j("#lyrVideo").fadeOut();
        j(".switchBigPic").fadeIn();
        j("#lyrCorners").fadeIn();
        j("#imgLink > #imgCorners").unwrap();
        j("#imgCorners").wrap("<a id='imgLink' href='"+g.find('.nav').children('li').eq(current).children('a').attr('href')+"' target='"+g.find('.nav').children('li').eq(current).children('a').attr('target')+"'></a>");
        g.find(".nav").children("li").css("background-image","url("+directory+"flashtext-bg.jpg)").css("z-index", 11);
        g.find(".nav").children("li").eq(current).css("background-image","url("+directory2+"flash-on.png)").css("border","none").css("z-index", 12);
        g.find(".switchBigPic").children("div").fadeOut();
        g.find(".switchBigPic").children("div").eq(current).fadeIn();
        g.find(".nav").children("li").eq(current).animate({marginLeft: '-30px', width: '220px'}, "fast");
        g.find(".nav").children("li").eq(w).animate({marginLeft: '0px', width: '190px'}, "fast");
        g.find(".nav").children("li").children("a").css("font-weight","normal").css("padding-left", "15px").css("width", "152px");
        g.find(".nav").children("li").children("a").eq(current).css("font-weight","bold").css("padding-left", "27px").css("width", "152px");
        g.find(".nav").children("li").children("a").children("div").attr("class", "desDesc");
        g.find(".nav").children("li").children("a").children("div").eq(current).attr("class", "desDesc");
        
        w = current;

    }
    
   play =  function(){
//window.status = y.toString();
            current++;
            if(current>=y){
                current=0
            }
            showpic();
            v=setTimeout(play,time_Interval);
    }
        
    init1 = function (){
        
        init2();
        y=g.children(".switchBigPic").children("div").length;
        
        return y;
        /*jQuery.support.cors = true;
        
        $.ajax({
            url: "http://gdata.youtube.com/feeds/api/videos", 
            data: "max-results=5&orderby=published&author=PBCountySheriff&alt=atom&start-index=1", 
            type: "GET", 
            datatype: "XML", 
            cache: "false", 
            success: parseMe,
            error: function(jqXHR, textStatus, errorThrown){
                alert("error: "+errorThrown+"\nDetails: "+textStatus);
             }, 
            contentType: "text/xml; charset=\"utf-8\""
        });
       return;
       */
    }
    /*
    parseMe = function (data, textStatus, jqXHR){ //alert("parseMe");
            $xml = $(data).children("feed");
            $xml.find("entry").each(function(index){
                var ww = $(this).find("content").text();
                var wt = $(this).children("title").text();
                var wm = $(this).find("media\\:thumbnail").attr("url");
                var wdesc = $(this).find("media\\:description").text();
                var wvid = $(this).find("media\\:content").attr("url");
                g.find(".switchBigPic").append("<div><a href=\'\#\'><img class=\'pic\' src=\'"+wm+"\'></a><p><b>"+wt+"</b><br>"+wdesc+"</p></div>");
                g.find(".nav").append("<li><a href=\'javascript:;\'>"+wt+"<br><div class=\'desDesc\'>"+ww+"</div></a></li>");
                vids.push(wvid);
            });
          y=g.children(".switchBigPic").children("div").length;
        init2();
        return y;
    }
    */
    function init2(){// alert("init2");
        g.css("width", slideshow_width+"px").css("height", slideshow_height+"px").css("position", "relative").css("font-family", "Arial, Helvetica, sans-serif").css("overflow", "hidden").css("left", "0px").css("text-align", "left");
        g.find("a").css("color", "#000");
        g.find("img").css("border", "none");
        j("#lyrCorners").css("z-index", 1);
        g.find("ul").css("margin", "0px").css("padding", "0px").css("background-color", "#CCCCCC").css("border-top", "1px solid #CCCCCC");
        g.find("li").css("margin", "0px").css("padding", "0px").css("list-style", "none");
        g.find(".switchBigPic").css("position", "absolute").css("z-index", 1).css("overflow", "hidden").css("height", "330px").css("width", "482px").css("left", "0px");
        g.find(".switchBigPic").children("div").css("position", "absolute").css("overflow", "hidden").css("left", "0px").css("width", "502px");
        g.find(".switchBigPic").children("div").children("p").css("position", "absolute").css("padding-top", "31px").css("padding-right", "5px").css("padding-bottom", "10px").css("padding-left", "70px").css("margin", "0px").css("left", "0px").css("bottom", "0px").css("background-image", "url("+directory+"descBG.png)").css("color", "#000000").css("font-size", "12px").css("line-height", "12px").css("width", "488px");
        g.find(".nav").css("width", "191px").css("height", "330px").css("position", "absolute").css("right", "0px").css("z-index", 10);
        g.find(".nav").children("li").css("height", "54px").css("width", thumbnail_width+"px").css("background-image", "url("+directory+"flashtext-bg.jpg)").css("line-height", "14px");
        g.find(".nav").children("li").children("a").css("text-decoration","none").css("height","40px").css("width", "152px").css("display","block").css("padding","10px").css("padding-left","15px").css("padding-bottom","0px").css("margin", "0px").css("font-size","12px").css("text-align", "left").css("overflow", "hidden");
        g.find(".nav").children("li").children("a").children("div").css("className", "desDesc");
        g.find(".nav").children("li").css("margin-left", "0px");
        g.find(".switchBigPic").children("div").hide();
        j(".desSlideshow").css("left", "0px").css("overflow", "visible");

        if(autoplay == "enable"){
            g.find(".switchBigPic").css("display","block");
            g.find(".nav").css("display", "block");
            g.css("background", "none");
            play();
        }else{
            g.find(".switchBigPic").css("display", "block");
            g.find(".nav").css("display", "block");
            g.css("background", "none");
            current = 0;
            showpic();
        }
        
        g.find(".nav").children("li").hover(
            function(){
                var index = g.find(".nav").children("li").index($(this));
                if(index != current){
                    current = index;
                    
                    showpic();
                }else{
                }
            },
            function(){
                    
            }
        );
        g.find(".nav").children("li").click(
            function(){
                var index = g.find(".nav").children("li").index($(this));
                j("#lyrVideo").fadeIn();
                j(".switchBigPic").fadeOut();
                j("#lyrCorners").fadeOut();
                playing = 1;
                g.stop();
                //playVideo(index);
            }
        );
        g.hover(
            function(){
            
            },
            function(){
                if(autoplay == "enable"){
                    if(playing == 0){
                        v=setTimeout(play,time_Interval);
                    }else{
                        v=setTimeout(play,time_Interval2);
                        playing = 0;
                        
                    }
                }
            }
        );
        
        return;
    }


playVideo = function(entryIndex) {
  var entry;
  //try {
    var videoHref = vids[entryIndex]; //findMediaContentHref(entry, "application/x-shockwave-flash");
    var videoPlayerDiv = document.getElementById('lyrVideo');
    // Add standard YouTube video embed code, with standard height/width
    // values.  Enable autoplay of the video.
    var html = [];
    //html.push('<b>');
    //html.push(entry.media$group.media$title.$t);
    //html.push('</b><br />');
    var vidW = slideshow_width - thumbnail_width;
    html.push('<object width="'+vidW+'" height="'+slideshow_height+'"><param name="movie" value="');
    html.push(videoHref);
    html.push('&autoplay=0"></param><param name="wmode" value="transparent">');
    html.push('</param><embed src="');
    html.push(videoHref);
    html.push('&autoplay=0" type="application/x-shockwave-flash" ');
    html.push('wmode="transparent" width="'+vidW+'" height="'+slideshow_height+'"></embed></object>');
    videoPlayerDiv.innerHTML = html.join('');
    //ytvb.showRelatedVideos(entryIndex, referringFeed);
   // if (entry.author && entry.author.length > 0) {
   //   ytvb.showVideosByUser(entry.author[0].uri.$t);
    //}
  //} catch (err) {
   // alert(err);
  //}
  return;
};

j(function(a) {
    
    a.fn.desSlideshow = function(p) {
        p=p||{};

        autoplay=p&&p.autoplay?p.autoplay:"enable";
        var slideshow_width1=p&&p.slideshow_width?p.slideshow_width:"600";
        var slideshow_height1=p&&p.slideshow_height?p.slideshow_height:"250";
        var thumbnail_width1=p&&p.thumbnail_width?p.thumbnail_width:"200";
        time_Interval = p&&p.time_Interval?p.time_Interval:"4000";
        time_Interval2 = p&&p.time_Interval2?p.time_Interval2:"8000";
        directory=p&&p.directory?p.directory:"images";
        directory2=p&&p.directory2?p.directory2:"images";
        slideshow_height = parseInt(slideshow_height1);
        slideshow_width = parseInt(slideshow_width1);
        thumbnail_width = parseInt(thumbnail_width1);
        time_Interval = parseInt(time_Interval);
        g=a(this);
        y = 0;
        vids = [];
        playing = 0;
        current = 0;
         v="";
         w=0;
        //slideshow_x = (parseInt(g.offsetParent().width()-10) / 2) - parseInt(slideshow_width / 2);
        slideshow_x = parseInt(g.offsetParent().position().left);
        init1();        
        
        if(y==0){
            g.append("Require content");
            return null
        }
        
    }
});

