﻿
    var objLastToggledItem = -1;
    function toggleDivNews(objItemID){

        if (objLastToggledItem!=-1) {
            if (objItemID==objLastToggledItem) {
                objLastToggledItem=-1;
                $('#' + objItemID).toggle();
            }else{
                var clone = $('#' + objItemID + 'Main').clone();
                $('#' + objItemID + 'Main').remove();
                clone.insertAfter($('#divRSSTop'));
                
                $('#' + objLastToggledItem).toggle();
                $('#' + objItemID).toggle();
                objLastToggledItem = objItemID; 
                
                         
            }
        }else{
            //divRSSTop
            var clone = $('#' + objItemID + 'Main').clone();
            $('#' + objItemID + 'Main').remove();
            clone.insertAfter($('#divRSSTop'));
            $('#' + objItemID).toggle();
            objLastToggledItem = objItemID;  
        }
    }
    
    function getContent(ContentID){
        var contentURL = '/ajx/GetContent.ashx?ContentID=' + ContentID;
        $("#divAuthorList").hide();
        $.getJSON(contentURL, function(JSON) {
            //alert(JSON.objContent.Header);

            $("#frameContent").attr("src", JSON.objContent.ResourceURL);
            //$("#contHeader").html(JSON.objContent.Header);
            $("#spnHeader").html(JSON.objContent.Header);
            $("#DateTimeAndNewsSourcesHTML").html(JSON.objContent.DateTimeAndNewsSourcesHTML + "");
            $("#spnResourceName").html(JSON.objContent.ResourceName);

            if (JSON.objContent.IsRSS == 1) {
                var frameURL = JSON.objContent.ResourceURL
                if (JSON.objContent.ResourceID == 25 | JSON.objContent.ResourceID == 26) frameURL = "/ShowWebPage.aspx?ContentID=" + ContentID;
                
                $("#frameContent").show();
                $("#frameContent").attr("src", frameURL);
                $("#divNormalContent").html("");
                setContentWitdh();
            } else {
                $("#divNormalContent").html(JSON.objContent.Content);
                $("#frameContent").hide();
            }
            var trackURL = "/trackRSSContent.aspx?ContentID=" + ContentID + "&Header=" + JSON.objContent.Header;
            pageTracker._trackPageview("/ajaxTrack/ContentID/" + ContentID + "/?header=" + JSON.objContent.Header);

        });



        return false;
    }
    var b='';
    var activeResourceID = -1;
    $(document).ready(function(){
        $.each($.browser, function(i, val) {
            if (i=='safari' && val==true){b='safari';}
            if (i=='opera' && val==true){b='opera';}
            if (i=='msie' && val==true){b='msie';}
            if (i=='mozilla' && val==true){b='mozilla';}
        });

        resizeContentWidth();
        
        var he = $("#divContentMain").height();
        $("#divContent").height(he - 20);
        
        
        $("#slCategoryList").change(function(){
            getContentList(activeResourceID,$(this).val());
        });
    });
    
    function getResourceContentList(ResourceID){
        activeResourceID = ResourceID;
        getContentList(ResourceID,-1)
        debugger;
        var CategoryURL = "/ajx/GetResourceRSSCategoryList.ashx?ResourceID=" + ResourceID;
        $.getJSON(CategoryURL,function(JSON){
            if (JSON!=null){
                var options = "";
                 for (var i = 0; i < JSON.length; i++) {
                    options += '<option value="' + JSON[i].CategoryID + '">' + JSON[i].Name + '</option>';
                  }
                  options = "<option value='-1'>Kategori seçiniz</option>" + options;
                  $("#slCategoryList").html(options);
                  $("#spnCategoryList").show();
            }
        });
    }
    
    function getContentList(ResourceID,CategoryID){
       var ContentListURL = "/ajx/GetRSSContentList.ashx?ResourceID=" + ResourceID + "&CategoryID=" + CategoryID;
       $.getJSON(ContentListURL,function(JSON){
            $("#divRSSDefaultContent").hide();
            if (JSON.ContentList!=null){
                var LinkList = "";

                for(var i=0; i < JSON.ContentList.length; i++){
                    //<span><a href="" onclick="getContent();return false;"></a></span>
                    LinkList += "<span><a href=\"" + JSON.ContentList[i].Url + "\"";
                   // if (ResourceID=="25"){
                   //     LinkList += " target=\"_blank\" ";
                   // }else{
                        LinkList += "onclick=\"getContent(" + JSON.ContentList[i].ContentID + "); return false;\"";
                   // }
                    
                    LinkList += " onmouseover=\"ddrivetip('" + JSON.ContentList[i].ToolTip + "')\"";
                    LinkList += " onmouseout=\"hideddrivetip();\"";
                        
                    LinkList += ">" + JSON.ContentList[i].Header + "</a></span>";
                }
                
                
                $("#divRSSContentLinks").html(LinkList);
                $("#divRSSContent").show();
                $("#divRSSContentResource").html(JSON.SourceName);
                $("#hdSelectFocus").focus();
            }
       });
    }
    

    
    function showDefaultRSS(){
        $('#divRSSContent').hide();
        $('#divRSSDefaultContent').show();
        $('#spnCategoryList').hide();
        activeResourceID=-1;
    }
    function resizeContentWidth(){
           setContentWitdh();

            var resizeTimer = null;
            $(window).bind('resize', function() {
                if (resizeTimer) clearTimeout(resizeTimer);
                resizeTimer = setTimeout(setContentWitdh, 100);
            });
     }

       function setContentWitdh() {
       
           var wt = $("#divWidth").width();
           var setWidth = 750;
             
          setWidth = wt - 250;

          
           if (b!='msie'){
               $(".TidyUp,.header,.menu,.siteFooter,#header").width(wt - 10);
               $(".SubNavigation-bot").width(wt-20);
               var contWT = wt - 20;
               $("#container").width(contWT);
               $("#divContent").width(setWidth);
           } else {
                if ($.browser.version>6){
                   $(".TidyUp").width(wt - 15);
                   $(".siteFooter").width(wt-5);
                    $(".SubNavigation-bot").width(wt-15);
                   var contWT = wt - 16;
                   $(".menu").width(contWT + 10);
                   $("#header").width(contWT + 6);
                   $("#container").width(contWT);
                   $("#divContent").width(setWidth);
                    
                }else{
                   $(".TidyUp,.siteFooter").width(wt - 15);
                   $(".SubNavigation-bot").width(wt-15);
                   var contWT = wt - 10;
                   $(".menu").width(contWT + 10);
                   $("#header").width(contWT + 6);
                   $("#container").width(contWT);
                   $("#divContent").width(setWidth);
                }
            }

            
       }

       function LoadAuthorData(objId) {
            var AuthorID = $('#' + objId).val();
            var AuthorName = $('#' + objId + ' :selected').text();
     
            var adres = "/ajx/GetAuthorLastContentID.ashx?AuthorID=" + AuthorID;
            $.getJSON(adres, function(ContentID) {
                if (ContentID != null) {
                    getContent(ContentID);

                    var authorData = getAuthorData(AuthorID,1);

                    var ls = AuthorListTemplate(AuthorName, "999999999", authorData, 0, false, false);
                    $("#divRSSDefaultContent").html(ls);
                    ResizeFrameHeight();
                }
            });
            
          
       }
       
        function getAuthorData(AuthorID,IsHeaderHaveSourceName){
            var retVal = "";
            
            var cListAddrs = "/ajx/GetContentList.ashx?AuthorID=" + AuthorID + "&IsHeaderHaveSourceName=" + IsHeaderHaveSourceName;

            $.ajax({
                async: false,
                url: cListAddrs,
                dataType: 'json',
                success: function(ContentList) {
                   var LinkList = "";
                   
                   if (ContentList!=null){
                    for(var i=0; i < ContentList.length; i++){
                        //<span><a href="" onclick="getContent();return false;"></a></span>
                        LinkList += "<span><a href=\"" + ContentList[i].Url + "\"";
                        LinkList += "onclick=\"getContent(" + ContentList[i].ContentID + "); return false;\"";
                        
                        LinkList += " onmouseover=\"ddrivetip('" + ContentList[i].ToolTip + "')\"";
                        LinkList += " onmouseout=\"hideddrivetip();\"";
                            
                        LinkList += ">" + ContentList[i].Header + "</a></span>";
                    }
                    retVal = LinkList;
                   }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {retVal= "";}
            });

            return retVal;
        }
        
        function SearchArticle(){
             var SearchText = $("#txtArticleText").val();
             if (SearchText!="Köşe yazıları arşivinde ara!") document.location.href="/Search.aspx?SearchText=" + SearchText + "&CategoryID=31";
        }
        function SearchOnKeyPress(e){
             if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)){
                SearchArticle();
                return false;
             }
             return true;
        }
        function AuthorListTemplate(AuthorName,AuthorID,htmlContent,itemIndex,toogleEnable,afterLoad){
            var disp = "none;";
            if (itemIndex==0){
                 disp="block;";
                 objLastToggledItem = "divResource" + AuthorID;
            }
            
            var retVal = "";
            retVal += "<div id=\"divResource" + AuthorID + "Main\">";
            retVal += "<div class=\"TitleDiv TitleGenel newsdetail-title\" style=\"w\\idth:205px;\"><span style=\"cursor:pointer;float:left;padding-left:0px;color:#fff;font-size:12px;\"";   
            
            var onClc = "";
            if (afterLoad)
            {
                onClc +="LoadSourceAuthorData(\'divResource" + + AuthorID + "\',\'" + AuthorID + "\'); ";
            }
            
            if (toogleEnable){
                //retVal +="onclick=\"toggleDivNews(\'divResource" + AuthorID  + "\');\"";
                onClc += "toggleDivNews(\'divResource" + AuthorID  + "\');";
            }
            
            if (onClc!=""){
                retVal +="onclick=\"" + onClc + "\"";
            }
            
            retVal += ">" + AuthorName + "</span></div>";
            retVal +="<div class=\"SubRightItem\" style=\"width: 220px;w\\idth: 215px; height: auto; display:" + disp + " \" id=\"divResource" + AuthorID + "\">";
            retVal +=htmlContent;
            retVal +="</div></div>";
            
            return retVal;
        }
        
        function LoadSourceAuthorData(loadObjID,AuthorID){
            var data = getAuthorData(AuthorID,1);
            $("#" + loadObjID).html(data);
        }
        
        function GetResourceLastContentOfAuthors(ResourceID){
            var objDDL = $("#ctl00$ContentPlaceHolder1$ddlArticleSourceList");
            objDDL.attr("disabled","true");
        
            var Adres = "/ajx/GetResourceAuthorWithLastContents.ashx?ResourceID=" +ResourceID ;
            $("#spnLoading").show();
            $.getJSON(Adres, function(AuthorList) {
                var allList = "";
                if (AuthorList != null) {
                    for (var i = 0; i < AuthorList.length; i++) {
                        var afterLoad = true;
                        var htmlContent = GetContentList(AuthorList[i].ContentList);

                        if (AuthorList[i].ContentList != null) afterLoad = false;

                        allList += AuthorListTemplate(AuthorList[i].AuthorName, AuthorList[i].AuthorID, htmlContent, i, true, afterLoad);
                    }
                }


                $("#divRSSDefaultContent").html('<span id="divRSSTop"></span><span id="divLastResourceAuthor"></span>');
                $("#divLastResourceAuthor").html(allList);

                ResizeFrameHeight();
                objDDL.attr("disabled", "");
                $("#spnLoading").hide();
            });
        }

        function ResizeFrameHeight() {
            var defHe = 0;
            if (b == 'msie') {
                defHe = $("#divLastResourceAuthor").height();
                defHe = defHe - 25;
            }
            else {
                defHe = $("#divContentMain").height();
                defHe = defHe - 30;
            }

            if (defHe >= 690) {
                $("#frameContent").height(defHe);
            }
            else {
               $("#frameContent").height(690);
            }
        
        }
        
        function GetContentList(ContentList){
            var LinkList = "";
            if (ContentList!=null){
                    for(var i=0; i < ContentList.length; i++){
                    //<span><a href="" onclick="getContent();return false;"></a></span>
                    LinkList += "<span><a href=\"" + ContentList[i].Url + "\"";
                    LinkList += "onclick=\"getContent(" + ContentList[i].ContentID + "); return false;\"";
                    
                    LinkList += " onmouseover=\"ddrivetip('" + ContentList[i].ToolTip + "')\"";
                    LinkList += " onmouseout=\"hideddrivetip();\"";
                        
                    LinkList += ">" + ContentList[i].Header + "</a></span>";
                }
            }
            
            return LinkList;
        }

        function LoadDefaultView(ResourceTypeID, NewsDivID) {
           //alert(ResourceTypeID);
            var ajxURL = '/ajx/ResourceTypeDefaultView.aspx?ResourceTypeID=' + ResourceTypeID;
            //$("#divRSSDefaultContent").load(ajxURL);

            $.ajax({
                async: false,
                url: ajxURL,
                success: function(content) {
                    $("#divRSSDefaultContent").html('<span id="divRSSTop"></span><span id="divLastResourceAuthor"></span>' + content);
                    objLastToggledItem = -1;
                    toggleDivNews(NewsDivID);
                }
            });

            return false;   
        }
