$(document).ready(function () {
    pageName = gParse('c');
    if (pageName == "") {
        QueryLoader.selectorPreload = "body";
        QueryLoader.init();
    }

    pageName == "" ? pageName = 'portfolio' : ""
    pageName == 'portfolio' ? $("#tabsDiv").show() : $("#tabsDiv").hide(); // Hide tabs;
    $("#holderDiv").html("");
    $("#holderDiv").hide();
    $("#" + pageName).addClass('selected'); //add select class to top menu
    $("#" + pageName).attr('onmouseout', "$(this).css({color:'#FFCD35'})")

    if (pageName == "contact") { // if resume load resume.html
        $.ajax({
            url: "contact.htm",
            cache: false,
            success: function (htmls) {
                $("#holderDiv").html(htmls);
                $("#holderDiv").fadeIn(600);
                $("#submitIMG").hover(function () {
                    $(this).attr("src", "images/sendOver.jpg")
                }, function () {
                    $(this).attr("src", "images/send.jpg")
                })

                $("#email").mouseover(function () {
                    $("#email").attr("href", "mailto:idohad@gmail.com?subject=" + $("#csubject").val())
                })

                $("#commentForm").validate({
                    submitHandler: function () {
                        $("#loadingIMG").fadeIn(600);
                        doAjax()
                    }
                });

                function doAjax() {
                    var subject = $("#csubject").val();
                    var name = $("#cname").val();
                    var email = $("#cemail").val();
                    var comment = $("#ccomment").val();
                    $.ajax({
                        type: "POST",
                        url: "http://idohad.com/sendemail.asp",
                        data: "subject=" + subject + "&name=" + name + "&email=" + email + "&comment=" + escape(comment),
                        success: function (msg) {
                            $(".popupBack").fadeIn(600);
                            $("#loadingIMG").fadeOut(600, function () {
                                $(".sentMsg").fadeIn(600);
                            });
                        }
                    });
                }

                $(".sentMsg").click(function () {
                    $(".popupBack").hide(600);
                    $(".sentMsg").hide(600);
                    $("#contactUs .required").val("");
                    $("#contactUs #csubject").val("Inquiry");
                })
            }
        });
        return
    };

    if (pageName == "resume" || pageName == "about") { // if resume load resume.html
        $.ajax({
            url: "resume.htm",
            cache: false,
            success: function (htmlq) {
                $("#holderDiv").html(htmlq);
                $("#holderDiv").fadeIn(600);
            }
        });
        return
    };

    if (pageName == "blog") { // if resume load resume.html
        $.ajax({
            url: "blog.htm",
            cache: false,
            success: function (htmlq) {
                $("#holderDiv").html(htmlq);
                $("#holderDiv").fadeIn(600);
            }
        });
        return
    };

    //Default Action
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content
    //On MouseOver Event
    $("ul.tabs li").mouseover(function () {
        $(this).css("background-position", "0 -87px"); //Remove any "active" class
    })

    $("ul.tabs li").mouseout(function () {
        $(this).css("background-position", "0 3px"); //Remove any "active" class
    })

    //On Click Event
    $("ul.tabs li").click(function () {
        tabNum = $(this)[0].value;
        // console.log($("this)[0].id")
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).fadeIn(900, function () {
            $(this).addClass("active")
        }) //Add "active" class to selected tab
        $("div.tablepager-links").html("");
        $("#holderDiv").html("");
        $("#holderDiv").hide(); //Hide all tab content

        if ($(this)[0].id == "more") { // if the tab is 'more' then load more.htm and stop script
            $.ajax({
                url: "more.htm",
                cache: false,
                success: function (htmlq) {
                    $("#holderDiv").html(htmlq);
                    $("#holderDiv").fadeIn(600);
                }
            });
            return
        };
        mis = tabNum + 1;
        s = 0;
        cdcd(mis)
    });
    gParse('catNum') != "" ? catNum = gParse('catNum') : "catNum=0";
    pageName == null ? pageName = 'portfolio' : ""
    var subXML = 'data/sub.xml';
    $.get(subXML, function (xmlS) {
        jsonSub = $.xml2json(xmlS);
        i = 0;
        s = 0;
        cdcd(2);
    })
    function cdcd(mis) {
        limit = 0;
        str = "<ul class='gallery'>";
        while (jsonSub.sub[s] != undefined) {
            IsDisableSub = jsonSub.sub[s].disable;
            subName = jsonSub.sub[s].subName;
            ScatID = jsonSub.sub[s].catID;
            if (IsDisableSub != 'True') {
                ScatID == mis ? ChangeCenter('jsonSub', s) : "";
            }
            s++;
        }
        str += "</ul>";
        c = 0;
        $("#holderDiv").html(str)
        $(".gallery").quickPager({
            pagerLocation: "before"
        })
        $("#holderDiv").fadeIn(600, function () {
            ////$(".gallery li").length > 4 ? $(".gallery").quickPager({ pagerLocation: "before" }) : "";
            });
    }

    function ChangeCenter(jsonType, id) {
        description = jsonSub.sub[id].description;
        name = jsonSub.sub[id].subName;
        divImageID = jsonSub.sub[id].divImageID;
        jsonType == 'jsonCat' ? catID = jsonCat.cat[id].catID : catID = null;
        imgGallery = divImageID != -1 ? "images/project" + divImageID + ".jpg" : "images/imgGallery.jpg";
        ////c % 2 == 0 ? str += "</ul>" : "";
        c++; // alert(id)
        str += "<li class='GalleryDivContent'><h1>" + name + "</h1><a href='#' onclick='doPopup(" + id + ")'> <img src='" + imgGallery + "' alt='' ></a><div class='descDiv'>" + description + "</div></a></li>";
    }

    function back(isScroll) {
        $("#innerText").hide(0);
        $("#innerText").fadeIn(0);
        if (isScroll != 0) { //
            $('.jScrollPaneContainer').css({
                'height': '150px'
            })
            $('#innerText').jScrollPane({
                showArrows: true,
                scrollbarWidth: 16,
                reinitialiseOnImageLoad: true
            });
        } else {
            $('.jScrollPaneContainer').css({
                'height': '400px'
            }); //
            $('#innerText').jScrollPane({
                showArrows: true,
                scrollbarWidth: 16,
                reinitialiseOnImageLoad: true
            });
        }
    }
});
var IsDisableCat = null;
var IsDisableSub = null;
//  var catXML = null;
var subXML = null;
var temp = "";
var ScatID = null;
var catID = null;
var title = null;
var subName = null;
var s = 0;
// var i = 0;
var description = null;
var jsonSub = null;
var jsonCat = null;
// var subCat = 1;
var isSroll = 1;
var catNum = 0;
var mis = null; // Default tab select
var tabNum = null;
var pageName = null;
var limit = 0;
var str = "";
var jsonType="";
var c = 0;
var str2="";
var idTemp=null;
var idd=null;
var divImageIDCash=null;
var htmls=null;
var htmlq=null;

function doPopup(id) {
    if($.browser.opera) {
        windowHeight = window.innerHeight;
        windowWidth = window.innerWidth;
    }else{
        windowHeight = $(window).height();
        windowWidth = $(window).width();
    };
    $(".popupBack").fadeIn(400, function () {
        $(".popup").fadeIn(500,function(){
            getPopupData(id);
            $("img.pagingImg").fadeIn(1500)
        })
    }
    );
}
function getPopupData(id) {
    id=="next" || id=="prev" ?$("#pagingPopup").html("")  :"";
    id=="next"?id=idTemp+1:idTemp;
    id=="prev"?id=idTemp-1:idTemp;
    subName = jsonSub.sub[id].subName;
    ScatID = jsonSub.sub[id].catID;
    description = jsonSub.sub[id].description;
    name = jsonSub.sub[id].subName;
    divImageID = jsonSub.sub[id].divImageID;
    imgGallery = divImageID != -1 ? "images/project" + divImageID + ".jpg" : "images/imgGallery.jpg";
    jsonSub.sub[id].description1!=undefined ? description1 =jsonSub.sub[id].description1:description1="";
    jsonSub.sub[id].description2!=undefined ? description2 =jsonSub.sub[id].description2:description2="";
    jsonSub.sub[id].description3!=undefined ? description3 =jsonSub.sub[id].description3:description3="";
    jsonSub.sub[id].openFile1!="" && jsonSub.sub[id].openFile1!=undefined ? openFile1 ="<a target='_blank' href="+jsonSub.sub[id].openFile1+"><img src='images/openFile.gif' style='float:right;width:22px;height:23px;padding-right:10px'</a>":openFile1="";
    jsonSub.sub[id].openFile2!="" && jsonSub.sub[id].openFile2!=undefined  ? openFile2 ="<a  target='_blank' href="+jsonSub.sub[id].openFile2+"><img src='images/openFile.gif' style='float:right;width:22px;height:23px;padding-right:10px'</a>":openFile2="";
    jsonSub.sub[id].openFile3!="" && jsonSub.sub[id].openFile3!=undefined  ? openFile3 ="<a  target='_blank' href="+jsonSub.sub[id].openFile3+"><img src='images/openFile.gif' style='float:right;width:22px;height:23px;padding-right:10px'</a>":openFile3="";
    str2 = "<table class=''>" +
    "<tr><td style='padding:20px 16px;'>" +
    "<img class='popupMainImg' id='popupMainImg' src='"+imgGallery+"'>" +
    "<img id='imgGalleryBIG' style='position:absolute;left:184px;top:42px;border:4px solid #999;display:none' src='"+imgGallery+"'>" +
    "<div class='popupTopDiv' style='float:left;width:600px;margin-top:13px;'>" +
    "<p style='font-size:12px;color:#f4f4f4;margin-bottom:10px;'>"+name+"</p><p style='color:#f4f4f4;margin-bottom:10px;'>"+description+"</p>" +
    "" +
    "" +
    "</div>" +
    "" +
    "</td></tr>" +
    "" +
    "" +
    "<tr><td><hr>"
    +"<div style='float:left;width:290px;' class='GalleryDivContentPopup'><a href='#' > <img onerror='this.src=&quot;images/imgGallery.jpg&quot;'  class='imgPopup' src='images/project"+divImageID+"_1.jpg' alt='' ></a><div class='popupDesc'><h1>Challenge</h1><p>" + description1 + "</p></div>"+openFile1+"</div>"
    +"<div style='float:left;width:290px;' class='GalleryDivContentPopup'><a href='#' > <img  onerror='this.src=&quot;images/imgGallery.jpg&quot;'  class='imgPopup' src='images/project"+divImageID+"_2.jpg' alt='' ></a><div class='popupDesc'><h1>Process</h1><p>" + description2 + "</p></div>"+openFile2+"</div>"
    +"<div style='float:left;width:290px;' class='GalleryDivContentPopup'><a href='#' > <img  onerror='this.src=&quot;images/imgGallery.jpg&quot;'  class='imgPopup' src='images/project"+divImageID+"_3.jpg' alt='' ></a><div class='popupDesc'><h1>Result</h1><p>" + description3 + "</p></div>"+openFile3+"</div>"
    +"</td></tr></table>";
    idd=id+1;
    typeof(jsonSub.sub[idd])!="undefined" ? divImageIDCash = jsonSub.sub[idd].divImageID:"";
    $("#test").html("<img  class='imgPopup' src='images/project"+divImageIDCash+"_1.jpg' alt='' ><img  class='imgPopup' src='images/project"+divImageIDCash+"_2.jpg' alt='' ><img  class='imgPopup' src='images/project"+divImageIDCash+"_3.jpg' alt='' >");
    $("#holderDivPopup").fadeOut(300,function(){
        $("#holderDivPopup").html(str2);
    });
    setTimeout(function(){
        $("#holderDivPopup").fadeIn(1000,function(){ 
            $("#popupMainImg").hover(
                function(){
                    $("#imgGalleryBIG").fadeIn(400);
                },
                function(){
                    $("#imgGalleryBIG").fadeOut(400);
                }
                )
        });
        var idp=id+1;
        var idm=id-1;
        $(".pagingImg").attr("src","images/paging.png");
        jsonSub.sub[idp]==undefined?$(".pagingImg").attr("src","images/paging_next.png"):"";
        jsonSub.sub[idm]==undefined?$(".pagingImg").attr("src","images/paging_prev.png"):"";
    },100)
    idTemp=id;
}
function closePopup() {
    $("#holderDivPopup").html("");
    $(".popupBack").hide(0);
    $(".popup").hide(500);
}

function over(x) {
    $('#' + x).css({
        color: '#FF0000'
    });
}

function out(x) {
    $('#' + x).css({
        color: '#000'
    });
}

function gParse(Query) {
    // Get from url
    var regex = new RegExp("[\\?&]" + Query.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]") + "=([^&#]*)");
    return (regex.exec(window.location.href) == null) ? "" : regex.exec(window.location.href)[1];
}
