$(document).ready(function() {

	$("div.overlay span.close").click( function () {
		$(this).parents("div.overlay").hide("fast");
		return false;
	});

	$("#domenListTable td.domen a").click(function () {
		if ($(this).parents("tr").hasClass("viewDomen") ) {
			$(this).parents("tr").removeClass("viewDomen");
			$("#DomenInfo:visible").hide();
		} else {
			$("#DomenInfo:hidden").show();
			$(this).parents("tbody").children("tr").removeClass("viewDomen");
			$(this).parents("tr").addClass("viewDomen");
		}
		return false;
	});

	$("#closeDomenInfo").click(function () {
		$("#domenListTable tr.viewDomen").removeClass("viewDomen");
		$("#DomenInfo:visible").hide();
	});

	


	//**********************************
	//  collapse

    /*
     * Filter 'slide-up'
     */
    $("div#parametersSection a.collapse").click(function () {
		$(this).parents("div.sectionWrapp").slideUp("fast").next().show();
        var totalAmount = 5;
        var counter = 0;
        var str = '';

        $("div#formContainer div.col1 div[class='column first'] div.subSection:first input:radio:checked").each(function() {
            counter++;
            str += $(this).parents("p").find("label").text() + ', ';
        });

        $("div#formContainer div.col1 div[class='column first'] div.subSection:last input:checkbox:checked:lt(" + (totalAmount - counter) + ")").each(function() {
            counter++;
            str += $(this).parents("p").find("label").text() + ', ';
        });

        if (counter < 5) {
            $("div#formContainer div.col1 div[class='column'] div.subSection:first input:radio:checked").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5) {
            $("div#formContainer div.col1 div[class='column'] div.subSection input:radio[name='chosenSubcategories']:checked").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5) {
            $("div#formContainer div.col1 div[class='column'] div.subSection input:checkbox:checked:lt(" + (totalAmount - counter) + ")").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5) {
            $("#ChooseRegions input:checkbox:checked:lt(" + (totalAmount - counter) + ")").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5) {
            $("#ChooseAuditory input:checkbox:checked:lt(" + (totalAmount - counter) + ")").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5) {
            $("#ChooseActivity input:checkbox:checked:lt(" + (totalAmount - counter) + ")").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5) {
            $("#ChooseCategory input:checkbox:checked:lt(" + (totalAmount - counter) + ")").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5) {
            $("#ChooseCounters input:checkbox:checked:lt(" + (totalAmount - counter) + ")").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5) {
            $("#ChooseVisitors input:checkbox:checked:lt(" + (totalAmount - counter) + ")").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5) {
            $("#ChooseRating input:checkbox:checked:lt(" + (totalAmount - counter) + ")").each(function() {
                counter++;
                str += $(this).parents("p").find("label").text() + ', ';
            });
        }

        if (counter < 5 && $("#sDomen").attr('value') != '') {
                counter++;
                str += $("#sDomen").attr('value');
        }

        $("span#searchFilter").text(str.substring(0, str.length - 2) + '...');
        return false;
	});

    /*
     * Upper table 'slide-up'
     */
    $("div#domenListSection a.collapse").click(function () {
		$(this).parents("div.sectionWrapp").slideUp("fast").next().show();
        var sitesList = '';
        sitesList += $("input#filteredAmount").val() + ' ';
        sitesList += foundByFilterText + ' ';
        $("div#domenListTable table tbody tr:lt(5)").each(function() {
            sitesList += $("td.domen a.moreInfo", this).text() + ', '
        });
        sitesList = sitesList.substring(0, sitesList.length - 2) + ' ...';
        $("span#foundSites").text(sitesList);
        return false;
	});

    /*
     * Lower table 'slide-up'
     */
    $("div#selectedDomensSection a.collapse").click(function () {
		$(this).parents("div.sectionWrapp").slideUp("fast").next().show();
        var sitesList = '';
        var sitesAddedCount = $("table#selectedDomenListTable tbody tr").length;
        sitesList += sitesAddedCount + ' ' + sitesAddedText + ' ';
        $("table#selectedDomenListTable tbody tr:lt(5)").each(function() {
            sitesList += $("td.domen a.moreInfo", this).text() + ', '
        });
        sitesList = sitesList.substring(0, sitesList.length - 2) + ' ...';
        $("span#addedSites").text(sitesList);
        return false;
	});

	$("a.expand").click(function () {
		$(this).parents("div.collapsed").hide().prev().slideDown("fast");
		return false;
	});


	// **********************************
	// my mediaplans

	$("ul.mediaplansList div.col1").hover(
      function () {
			$(this).find("a.del").fadeIn(250);
      },
      function () {
        $(this).find("a.del").fadeOut(250);
      }
    );

	$("ul.mediaplansList div.col2 input:checked").parents("div.col2").find("a.link").show();

	$("ul.mediaplansList div.col2 input").click(function() {
		if (this.checked) { $(this).parents("div.col2").find("a.link").fadeIn(250); }
		else { $(this).parents("div.col2").find("a.link").fadeOut(250); }
	});

    //*********************************
    // Change mediaplan's name

    $("body").click(function() {
       $("#ChangeNameDiv").fadeOut("fast");
    });

    $("#changeName").click(function () {
        $("#chName").get(0).value = $("#planName").text();
        $("#ChangeNameDiv").fadeIn("fast");
        return false;
    });


    $("#chName").click(function () {
        return false;
    });

});